home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / modex104.zip / MODEX.ASM < prev    next >
Assembly Source File  |  1993-05-14  |  117KB  |  3,296 lines

  1. ;========================================================
  2. ; MODEX.ASM - A Complete Mode X Library
  3. ;
  4. ; Version 1.04 Release, 3 May 1993, By Matt Pritchard
  5. ; With considerable input from Michael Abrash
  6. ;
  7. ; The following information is donated to the public domain in
  8. ; the hopes that save other programmers much frustration.
  9. ;
  10. ; If you do use this code in a product, it would be nice if
  11. ; you include a line like "Mode X routines by Matt Pritchard"
  12. ; in the credits.
  13. ;
  14. ; =========================================================
  15. ;
  16. ; All of this code is designed to be assembled with MASM 5.10a
  17. ; but TASM 3.0 could be used as well.
  18. ;
  19. ; The routines contained are designed for use in a MEDIUM model
  20. ; program.  All Routines are FAR, and is assumed that a DGROUP
  21. ; data segment exists and that DS will point to it on entry.
  22. ;
  23. ; For all routines, the AX, BX, CX, DX, ES and FLAGS registers
  24. ; will not be preserved, while the DS, BP, SI and DI registers
  25. ; will be preserved.
  26. ;
  27. ; Unless specifically noted, All Parameters are assumed to be
  28. ; "PASSED BY VALUE".  That is, the actual value is placed on
  29. ; the stack.  When a reference is passed it is assumed to be
  30. ; a near pointer to a variable in the DGROUP segment.
  31. ;
  32. ; Routines that return a single 16-Bit integer value will
  33. ; return that value in the AX register.
  34. ;
  35. ; This code will *NOT* run on an 8086/8088 because 80286+
  36. ; specific instructions are used.   If you have an 8088/86
  37. ; and VGA, you can buy an 80386-40 motherboard for about
  38. ; $160 and move into the 90's.
  39. ;
  40. ; This code is reasonably optimized: Most drawing loops have
  41. ; been unrolled once and memory references are minimized by
  42. ; keeping stuff in registers when possible.
  43. ;
  44. ; Error Trapping varies by Routine.  No Clipping is performed
  45. ; so the caller should verify that all coordinates are valid.
  46. ;
  47. ; Several Macros are used to simplify common 2 or 3 instruction
  48. ; sequences.  Several Single letter Text Constants also
  49. ; simplify common assembler expressions like "WORD PTR".
  50. ;
  51. ; ------------------ Mode X Variations ------------------
  52. ;
  53. ;  Mode #  Screen Size    Max Pages   Aspect Ratio (X:Y)
  54. ;
  55. ;    0      320 x 200      4 Pages         1.2:1
  56. ;    1      320 x 400      2 Pages         2.4:1
  57. ;    2      360 x 200      3 Pages        1.35:1
  58. ;    3      360 x 400      1 Page          2.7:1
  59. ;    4      320 x 240      3 Pages           1:1
  60. ;    5      320 x 480      1 Page            2:1
  61. ;    6      360 x 240      3 Pages       1.125:1
  62. ;    7      360 x 480      1 Page         2.25:1
  63. ;
  64. ; -------------------- The Legal Stuff ------------------
  65. ;
  66. ; No warranty, either written or implied, is made as to
  67. ; the accuracy and usability of this code product.  Use
  68. ; at your own risk.  Batteries not included.  Pepperoni
  69. ; and extra cheese available for an additional charge.
  70. ;
  71. ; ----------------------- The Author --------------------
  72. ;
  73. ; Matt Pritchard is a paid programmer who'd rather be
  74. ; writing games.  He can be reached at: P.O. Box 140264,
  75. ; Irving, TX  75014  USA.  Michael Abrash is a living
  76. ; god, who now works for Bill Gates (Microsoft).
  77. ;
  78. ; -------------------- Revision History -----------------
  79. ; 4-12-93: v1.02 - SET_POINT & READ_POINT now saves DI
  80. ;          SET_MODEX now saves SI
  81. ; 5-3-93:  v1.04 - added LOAD_DAC_REGISTERS and
  82. ;          READ_DAC_REGISTERS.  Expanded CLR Macro
  83. ;          to handle multiple registers
  84. ;
  85.  
  86.     PAGE    255, 132
  87.  
  88.     .MODEL Medium
  89.     .286
  90.  
  91.     ; ===== MACROS =====
  92.  
  93.     ; Macro to OUT a 16 bit value to an I/O port
  94.  
  95. OUT_16 MACRO Register, Value
  96.     IFDIFI <Register>, <DX>         ; If DX not setup
  97.         MOV     DX, Register        ; then Select Register
  98.     ENDIF
  99.     IFDIFI <Value>, <AX>            ; If AX not setup
  100.         MOV     AX, Value           ; then Get Data Value
  101.     ENDIF
  102.         OUT     DX, AX              ; Set I/O Register(s)
  103. ENDM
  104.  
  105.     ; Macro to OUT a 8 bit value to an I/O Port
  106.  
  107. OUT_8 MACRO Register, Value
  108.     IFDIFI <Register>, <DX>         ; If DX not setup
  109.         MOV     DX, Register        ; then Select Register
  110.     ENDIF
  111.     IFDIFI <Value>, <AL>            ; If AL not Setup
  112.         MOV     AL, Value           ; then Get Data Value
  113.     ENDIF
  114.         OUT     DX, AL              ; Set I/O Register
  115. ENDM
  116.  
  117.     ; macros to PUSH and POP multiple registers
  118.  
  119. PUSHx MACRO R1, R2, R3, R4, R5, R6, R7, R8
  120.     IFNB <R1>
  121.         PUSH    R1              ; Save R1
  122.         PUSHx   R2, R3, R4, R5, R6, R7, R8
  123.     ENDIF
  124. ENDM
  125.  
  126. POPx MACRO R1, R2, R3, R4, R5, R6, R7, R8
  127.     IFNB <R1>
  128.         POP     R1              ; Restore R1
  129.         POPx    R2, R3, R4, R5, R6, R7, R8
  130.     ENDIF
  131. ENDM
  132.  
  133.     ; Macro to Clear Registers to 0
  134.  
  135. CLR MACRO Register, R2, R3, R4, R5, R6
  136.     IFNB <Register>
  137.         XOR     Register, Register      ; Set Register = 0
  138.         CLR R2, R3, R4, R5, R6
  139.     ENDIF
  140. ENDM
  141.  
  142.     ; Macros to Decrement Counter & Jump on Condition
  143.  
  144. LOOPx MACRO Register, Destination
  145.     DEC     Register                ; Counter--
  146.     JNZ     Destination             ; Jump if not 0
  147. ENDM
  148.  
  149. LOOPjz MACRO Register, Destination
  150.     DEC     Register                ; Counter--
  151.     JZ      Destination             ; Jump if 0
  152. ENDM
  153.  
  154.  
  155.     ; ===== General Constants =====
  156.  
  157.     False   EQU 0
  158.     True    EQU -1
  159.     nil     EQU 0
  160.  
  161.     b       EQU BYTE PTR
  162.     w       EQU WORD PTR
  163.     d       EQU DWORD PTR
  164.     o       EQU OFFSET
  165.     f       EQU FAR PTR
  166.     s       EQU SHORT
  167.     ?x4     EQU <?,?,?,?>
  168.     ?x3     EQU <?,?,?>
  169.  
  170.     ; ===== VGA Register Values =====
  171.  
  172.     VGA_Segment     EQU 0A000h  ; Vga Memory Segment
  173.  
  174.     ATTRIB_Ctrl     EQU 03C0h   ; VGA Attribute Controller
  175.     GC_Index        EQU 03CEh   ; VGA Graphics Controller
  176.     SC_Index        EQU 03C4h   ; VGA Sequencer Controller
  177.     SC_Data         EQU 03C5h   ; VGA Sequencer Data Port
  178.     CRTC_Index      EQU 03D4h   ; VGA CRT Controller
  179.     CRTC_Data       EQU 03D5h   ; VGA CRT Controller Data
  180.     MISC_OUTPUT     EQU 03C2h   ; VGA Misc Register
  181.     INPUT_1         EQU 03DAh   ; Input Status #1 Register
  182.  
  183.     DAC_WRITE_ADDR  EQU 03C8h   ; VGA DAC Write Addr Register
  184.     DAC_READ_ADDR   EQU 03C7h   ; VGA DAC Read Addr Register
  185.     PEL_DATA_REG    EQU 03C9h   ; VGA DAC/PEL data Register R/W
  186.  
  187.     PIXEL_PAN_REG   EQU 033h    ; Attrib Index: Pixel Pan Reg
  188.     MAP_MASK        EQU 002h    ; Sequ Index: Write Map Mask reg
  189.     READ_MAP        EQU 004h    ; GC Index: Read Map Register
  190.     START_DISP_HI   EQU 00Ch    ; CRTC Index: Display Start Hi
  191.     START_DISP_LO   EQU 00Dh    ; CRTC Index: Display Start Lo
  192.  
  193.     MAP_MASK_PLANE1 EQU 00102h  ; Map Register + Plane 1
  194.     MAP_MASK_PLANE2 EQU 01102h  ; Map Register + Plane 1
  195.     ALL_PLANES_ON   EQU 00F02h  ; Map Register + All Bit Planes
  196.  
  197.     CHAIN4_OFF      EQU 00604h  ; Chain 4 mode Off
  198.     ASYNC_RESET     EQU 00100h  ; (A)synchronous Reset
  199.     SEQU_RESTART    EQU 00300h  ; Sequencer Restart
  200.  
  201.     LATCHES_ON      EQU 00008h  ; Bit Mask + Data from Latches
  202.     LATCHES_OFF     EQU 0FF08h  ; Bit Mask + Data from CPU
  203.  
  204.     VERT_RETRACE    EQU 08h     ; INPUT_1: Vertical Retrace Bit
  205.     PLANE_BITS      EQU 03h     ; Bits 0-1 of Xpos = Plane #
  206.     ALL_PLANES      EQU 0Fh     ; All Bit Planes Selected
  207.     CHAR_BITS       EQU 0Fh     ; Bits 0-3 of Character Data
  208.  
  209.     GET_CHAR_PTR    EQU 01130h  ; VGA BIOS Func: Get Char Set
  210.     ROM_8x8_Lo      EQU 03h     ; ROM 8x8 Char Set Lo Pointer
  211.     ROM_8x8_Hi      EQU 04h     ; ROM 8x8 Char Set Hi Pointer
  212.  
  213.     ; Constants Specific for these routines
  214.  
  215.     NUM_MODES       EQU 8       ; # of Mode X Variations
  216.  
  217.     ; Specific Mode Data Table format...
  218.  
  219. Mode_Data_Table STRUC
  220.     M_MiscR         DB  ?       ; Value of MISC_OUTPUT register
  221.     M_Pages         DB  ?       ; Maximum Possible # of pages
  222.     M_XSize         DW  ?       ; X Size Displayed on screen
  223.     M_YSize         DW  ?       ; Y Size Displayed on screen
  224.     M_XMax          DW  ?       ; Maximum Possible X Size
  225.     M_YMax          DW  ?       ; Maximum Possible Y Size
  226.     M_CRTC          DW  ?       ; Table of CRTC register values
  227. Mode_Data_Table ENDS
  228.  
  229.     ; ===== DGROUP STORAGE NEEDED (42 BYTES) =====
  230.  
  231.     .DATA?
  232.  
  233. SCREEN_WIDTH    DW  0       ; Width of a line in Bytes
  234. SCREEN_HEIGHT   DW  0       ; Vertical Height in Pixels
  235.  
  236. LAST_PAGE       DW  0       ; # of Display Pages
  237. PAGE_ADDR       DW  4 DUP (0)   ; Offsets to start of each page
  238.  
  239. PAGE_SIZE       DW  0       ; Size of Page in Addr Bytes
  240.  
  241. DISPLAY_PAGE    DW  0       ; Page # currently displayed
  242. ACTIVE_PAGE     DW  0       ; Page # currently active
  243.  
  244. CURRENT_PAGE    DW  0       ; Offset of current Page
  245. CURRENT_SEGMENT DW  0       ; Segment of VGA memory
  246.  
  247. CURRENT_XOFFSET DW  0       ; Current Display X Offset
  248. CURRENT_YOFFSET DW  0       ; Current Display Y Offset
  249.  
  250. CURRENT_MOFFSET DW  0       ; Current Start Offset
  251.  
  252. MAX_XOFFSET     DW  0       ; Current Display X Offset
  253. MAX_YOFFSET     DW  0       ; Current Display Y Offset
  254.  
  255. CHARSET_LOW     DW  0, 0    ; Far Ptr to Char Set: 0-127
  256. CHARSET_HI      DW  0, 0    ; Far Ptr to Char Set: 128-255
  257.  
  258.     .CODE
  259.  
  260.     ; ===== DATA TABLES =====
  261.  
  262.     ; Data Tables, Put in Code Segment for Easy Access
  263.     ; (Like when all the other Segment Registers are in
  264.     ; use!!) and reduced DGROUP requirements...
  265.  
  266.     ; Bit Mask Tables for Left/Right/Character Masks
  267.  
  268. Left_Clip_Mask      DB  0FH, 0EH, 0CH, 08H
  269.  
  270. Right_Clip_Mask     DB  01H, 03H, 07H, 0FH
  271.  
  272.     ; Bit Patterns for converting character fonts
  273.  
  274. Char_Plane_Data     DB  00H,08H,04H,0CH,02H,0AH,06H,0EH
  275.                     DB  01H,09H,05H,0DH,03H,0BH,07H,0FH
  276.  
  277.         ; CRTC Register Values for Various Configurations
  278.  
  279. MODE_Single_Line:       ; CRTC Setup Data for 400/480 Line modes
  280.         DW  04009H      ; Cell Height (1 Scan Line)
  281.         DW  00014H      ; Dword Mode off
  282.         DW  0E317H      ; turn on Byte Mode
  283.         DW  nil         ; End of CRTC Data for 400/480 Line Mode
  284.  
  285. MODE_Double_Line:       ; CRTC Setup Data for 200/240 Line modes
  286.         DW  04109H      ; Cell Height (2 Scan Lines)
  287.         DW  00014H      ; Dword Mode off
  288.         DW  0E317H      ; turn on Byte Mode
  289.         DW  nil         ; End of CRTC Data for 200/240 Line Mode
  290.  
  291. MODE_320_Wide:          ; CRTC Setup Data for 320 Horz Pixels
  292.         DW  05F00H      ; Horz total
  293.         DW  04F01H      ; Horz Displayed
  294.         DW  05002H      ; Start Horz Blanking
  295.         DW  08203H      ; End Horz Blanking
  296.         DW  05404H      ; Start H Sync
  297.         DW  08005H      ; End H Sync
  298.         DW  nil         ; End of CRTC Data for 320 Horz pixels
  299.  
  300. MODE_360_Wide:          ; CRTC Setup Data for 360 Horz Pixels
  301.         DW  06B00H      ; Horz total
  302.         DW  05901H      ; Horz Displayed
  303.         DW  05A02H      ; Start Horz Blanking
  304.         DW  08E03H      ; End Horz Blanking
  305.         DW  05E04H      ; Start H Sync
  306.         DW  08A05H      ; End H Sync
  307.         DW  nil         ; End of CRTC Data for 360 Horz pixels
  308.  
  309. MODE_200_Tall:
  310. MODE_400_Tall:          ; CRTC Setup Data for 200/400 Line modes
  311.         DW  0BF06H      ; Vertical Total
  312.         DW  01F07H      ; Overflow
  313.         DW  09C10H      ; V Sync Start
  314.         DW  08E11H      ; V Sync End/Prot Cr0 Cr7
  315.         DW  08F12H      ; Vertical Displayed
  316.         DW  09615H      ; V Blank Start
  317.         DW  0B916H      ; V Blank End
  318.         DW  nil         ; End of CRTC Data for 200/400 Lines
  319.  
  320. MODE_240_Tall:
  321. MODE_480_Tall:          ; CRTC Setup Data for 240/480 Line modes
  322.         DW  00D06H      ; Vertical Total
  323.         DW  03E07H      ; Overflow
  324.         DW  0EA10H      ; V Sync Start
  325.         DW  08C11H      ; V Sync End/Prot Cr0 Cr7
  326.         DW  0DF12H      ; Vertical Displayed
  327.         DW  0E715H      ; V Blank Start
  328.         DW  00616H      ; V Blank End
  329.         DW  nil         ; End of CRTC Data for 240/480 Lines
  330.  
  331.         ; Table of Display Mode Tables
  332.  
  333. MODE_TABLE:
  334.         DW  o MODE_320x200, o MODE_320x400
  335.         DW  o MODE_360x200, o MODE_360x400
  336.         DW  o MODE_320x240, o MODE_320x480
  337.         DW  o MODE_360x240, o MODE_360x480
  338.  
  339.         ; Table of Display Mode Components
  340.  
  341. MODE_320x200:           ; Data for 320 by 200 Pixels
  342.  
  343.         DB  063h        ; 400 scan Lines & 25 Mhz Clock
  344.         DB  4           ; Maximum of 4 Pages
  345.         DW  320, 200    ; Displayed Pixels (X,Y)
  346.         DW  1302, 816   ; Max Possible X and Y Sizes
  347.  
  348.         DW  o MODE_320_Wide, o MODE_200_Tall
  349.         DW  o MODE_Double_Line, nil
  350.  
  351. MODE_320x400:           ; Data for 320 by 400 Pixels
  352.  
  353.         DB  063h        ; 400 scan Lines & 25 Mhz Clock
  354.         DB  2           ; Maximum of 2 Pages
  355.         DW  320, 400    ; Displayed Pixels X,Y
  356.         DW  648, 816    ; Max Possible X and Y Sizes
  357.  
  358.         DW  o MODE_320_Wide, o MODE_400_Tall
  359.         DW  o MODE_Single_Line, nil
  360.  
  361. MODE_360x240:           ; Data for 360 by 240 Pixels
  362.  
  363.         DB  0E7h        ; 480 scan Lines & 28 Mhz Clock
  364.         DB  3           ; Maximum of 3 Pages
  365.         DW  360, 240    ; Displayed Pixels X,Y
  366.         DW  1092, 728   ; Max Possible X and Y Sizes
  367.  
  368.         DW  o MODE_360_Wide, o MODE_240_Tall
  369.         DW  o MODE_Double_Line , nil
  370.  
  371. MODE_360x480:           ; Data for 360 by 480 Pixels
  372.  
  373.         DB  0E7h        ; 480 scan Lines & 28 Mhz Clock
  374.         DB  1           ; Only 1 Page Possible
  375.         DW  360, 480    ; Displayed Pixels X,Y
  376.         DW  544, 728    ; Max Possible X and Y Sizes
  377.  
  378.         DW  o MODE_360_Wide, o MODE_480_Tall
  379.         DW  o MODE_Single_Line , nil
  380.  
  381. MODE_320x240:           ; Data for 320 by 240 Pixels
  382.  
  383.         DB  0E3h        ; 480 scan Lines & 25 Mhz Clock
  384.         DB  3           ; Maximum of 3 Pages
  385.         DW  320, 240    ; Displayed Pixels X,Y
  386.         DW  1088, 818   ; Max Possible X and Y Sizes
  387.  
  388.         DW  o MODE_320_Wide, o MODE_240_Tall
  389.         DW  o MODE_Double_Line, nil
  390.  
  391. MODE_320x480:           ; Data for 320 by 480 Pixels
  392.  
  393.         DB  0E3h        ; 480 scan Lines & 25 Mhz Clock
  394.         DB  1           ; Only 1 Page Possible
  395.         DW  320, 480    ; Displayed Pixels X,Y
  396.         DW  540, 818    ; Max Possible X and Y Sizes
  397.  
  398.         DW  o MODE_320_WIDE, o MODE_480_Tall
  399.         DW  o MODE_Single_Line, nil
  400.  
  401. MODE_360x200:           ; Data for 360 by 200 Pixels
  402.  
  403.         DB  067h        ; 400 scan Lines & 28 Mhz Clock
  404.         DB  3           ; Maximum of 3 Pages
  405.         DW  360, 200    ; Displayed Pixels (X,Y)
  406.         DW  1302, 728   ; Max Possible X and Y Sizes
  407.  
  408.         DW  o MODE_360_Wide, MODE_200_Tall
  409.         DW  o MODE_Double_Line, nil
  410.  
  411. MODE_360x400:           ; Data for 360 by 400 Pixels
  412.  
  413.         DB  067h        ; 400 scan Lines & 28 Mhz Clock
  414.         DB  1           ; Maximum of 1 Pages
  415.         DW  360, 400    ; Displayed Pixels X,Y
  416.         DW  648, 816    ; Max Possible X and Y Sizes
  417.  
  418.         DW  o MODE_360_Wide, MODE_400_Tall
  419.         DW  o MODE_Single_Line, nil
  420.  
  421.  
  422.     ; ===== MODE X SETUP ROUTINES =====
  423.  
  424. ;======================================================
  425. ;SET_VGA_MODEX% (ModeType%, MaxXPos%, MaxYpos%, Pages%)
  426. ;======================================================
  427. ;
  428. ; Sets Up the specified version of Mode X.  Allows for
  429. ; the setup of multiple video pages, and a virtual
  430. ; screen which can be larger than the displayed screen
  431. ; (which can then be scrolled a pixel at a time)
  432. ;
  433. ; ENTRY: ModeType = Desired Screen Resolution (0-7)
  434. ;
  435. ;     0 =  320 x 200, 4 Pages max,   1.2:1 Aspect Ratio
  436. ;     1 =  320 x 400, 2 Pages max,   2.4:1 Aspect Ratio
  437. ;     2 =  360 x 200, 3 Pages max,  1.35:1 Aspect Ratio
  438. ;     3 =  360 x 400, 1 Page  max,   2.7:1 Aspect Ratio
  439. ;     4 =  320 x 240, 3 Pages max,     1:1 Aspect Ratio
  440. ;     5 =  320 x 480, 1 Page  max,     2:1 Aspect Ratio
  441. ;     6 =  360 x 240, 3 Pages max, 1.125:1 Aspect Ratio
  442. ;     7 =  360 x 480, 1 Page  max,  2.25:1 Aspect Ratio
  443. ;
  444. ;        MaxXpos = The Desired Virtual Screen Width
  445. ;        MaxYpos = The Desired Virtual Screen Height
  446. ;        Pages   = The Desired # of Video Pages
  447. ;
  448. ; EXIT:  AX = Success Flag:   0 = Failure / -1= Success
  449. ;
  450.  
  451. SVM_STACK   STRUC
  452.     SVM_Table   DW  ?   ; Offset of Mode Info Table
  453.                 DW  ?x4 ; DI, SI, DS, BP
  454.                 DD  ?   ; Caller
  455.     SVM_Pages   DW  ?   ; # of Screen Pages desired
  456.     SVM_Ysize   DW  ?   ; Vertical Screen Size Desired
  457.     SVM_Xsize   DW  ?   ; Horizontal Screen Size Desired
  458.     SVM_Mode    DW  ?   ; Display Resolution Desired
  459. SVM_STACK   ENDS
  460.  
  461.     PUBLIC  SET_VGA_MODEX
  462.  
  463. SET_VGA_MODEX   PROC    FAR
  464.  
  465.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  466.     SUB     SP, 2               ; Allocate workspace
  467.     MOV     BP, SP              ; Set up Stack Frame
  468.  
  469.     ; Check Legality of Mode Request....
  470.  
  471.     MOV     BX, [BP].SVM_Mode   ; Get Requested Mode #
  472.     CMP     BX, NUM_MODES       ; Is it 0..7?
  473.     JAE     @SVM_BadModeSetup   ; If Not, Error out
  474.  
  475.     SHL     BX, 1                   ; Scale BX
  476.     MOV     SI, w MODE_TABLE[BX]    ; CS:SI -> Mode Info
  477.     MOV     [BP].SVM_Table, SI      ; Save ptr for later use
  478.  
  479.     ; Check # of Requested Display Pages
  480.  
  481.     MOV     CX, [BP].SVM_Pages  ; Get # of Requested Pages
  482.     CLR     CH                  ; Set Hi Word = 0!
  483.     CMP     CL, CS:[SI].M_Pages ; Check # Pages for mode
  484.     JA      @SVM_BadModeSetup   ; Report Error if too Many Pages
  485.     JCXZ    @SVM_BadModeSetup   ; Report Error if 0 Pages
  486.  
  487.     ; Check Validity of X Size
  488.  
  489.     AND     [BP].SVM_XSize, 0FFF8h  ; X size Mod 8 Must = 0
  490.  
  491.     MOV     AX, [BP].SVM_XSize  ; Get Logical Screen Width
  492.     CMP     AX, CS:[SI].M_XSize ; Check against Displayed X
  493.     JB      @SVM_BadModeSetup   ; Report Error if too small
  494.     CMP     AX, CS:[SI].M_XMax  ; Check against Max X
  495.     JA      @SVM_BadModeSetup   ; Report Error if too big
  496.  
  497.     ; Check Validity of Y Size
  498.  
  499.     MOV     BX, [BP].SVM_YSize  ; Get Logical Screen Height
  500.     CMP     BX, CS:[SI].M_YSize ; Check against Displayed Y
  501.     JB      @SVM_BadModeSetup   ; Report Error if too small
  502.     CMP     BX, CS:[SI].M_YMax  ; Check against Max Y
  503.     JA      @SVM_BadModeSetup   ; Report Error if too big
  504.  
  505.     ; Enough memory to Fit it all?
  506.  
  507.     SHR     AX, 2               ; # of Bytes:Line = XSize/4
  508.     MUL     CX                  ; AX = Bytes/Line * Pages
  509.     MUL     BX                  ; DX:AX = Total VGA mem needed
  510.     JNO     @SVM_Continue       ; Exit if Total Size > 256K
  511.  
  512.     DEC     DX                  ; Was it Exactly 256K???
  513.     OR      DX, AX              ; (DX = 1, AX = 0000)
  514.     JZ      @SVM_Continue       ; if so, it's valid...
  515.  
  516. @SVM_BadModeSetup:
  517.  
  518.     CLR     AX                  ; Return Value = False
  519.     JMP     @SVM_Exit           ; Normal Exit
  520.  
  521. @SVM_Continue:
  522.  
  523.     MOV     AX, 13H             ; Start with Mode 13H
  524.     INT     10H                 ; Let BIOS Set Mode
  525.  
  526.     OUT_16  SC_INDEX, CHAIN4_OFF            ; Disable Chain 4 Mode
  527.     OUT_16  SC_INDEX, ASYNC_RESET           ; (A)synchronous Reset
  528.     OUT_8   MISC_OUTPUT, CS:[SI].M_MiscR    ; Set New Timing/Size
  529.     OUT_16  SC_INDEX, SEQU_RESTART          ; Restart Sequencer ...
  530.  
  531.     OUT_8   CRTC_INDEX, 11H     ; Select Vert Retrace End Register
  532.     INC     DX                  ; Point to Data
  533.     IN      AL, DX              ; Get Value, Bit 7 = Protect
  534.     AND     AL, 7FH             ; Mask out Write Protect
  535.     OUT     DX, AL              ; And send it back
  536.  
  537.     MOV     DX, CRTC_INDEX      ; Vga Crtc Registers
  538.     ADD     SI, M_CRTC          ; SI -> CRTC Parameter Data
  539.  
  540.     ; Load Tables of CRTC Parameters from List of Tables
  541.  
  542. @SVM_Setup_Table:
  543.  
  544.     MOV     DI, CS:[SI]         ; Get Pointer to CRTC Data Tbl
  545.     ADD     SI, 2               ; Point to next Ptr Entry
  546.     OR      DI, DI              ; A nil Ptr means that we have
  547.     JZ      @SVM_Set_Data       ; finished CRTC programming
  548.  
  549. @SVM_Setup_CRTC:
  550.     MOV     AX, CS:[DI]         ; Get CRTC Data from Table
  551.     ADD     DI, 2               ; Advance Pointer
  552.     OR      AX, AX              ; At End of Data Table?
  553.     JZ      @SVM_Setup_Table    ; If so, Exit & get next Table
  554.  
  555.     OUT     DX, AX              ; Reprogram VGA CRTC reg
  556.     JMP     s @SVM_Setup_CRTC   ; Process Next Table Entry
  557.  
  558.     ; Initialize Page & Scroll info, DI = 0
  559.  
  560. @SVM_Set_Data:
  561.     MOV     DISPLAY_PAGE, DI    ; Display Page = 0
  562.     MOV     ACTIVE_PAGE, DI     ; Active Page = 0
  563.     MOV     CURRENT_PAGE, DI    ; Current Page (Offset) = 0
  564.     MOV     CURRENT_XOFFSET, DI ; Horz Scroll Index = 0
  565.     MOV     CURRENT_YOFFSET, DI ; Vert Scroll Index = 0
  566.     MOV     CURRENT_MOFFSET, DI ; Memory Scroll Index = 0
  567.  
  568.     MOV     AX, VGA_SEGMENT     ; Segment for VGA memory
  569.     MOV     CURRENT_SEGMENT, AX ; Save for Future LES's
  570.  
  571.     ; Set Logical Screen Width, X Scroll and Our Data
  572.  
  573.     MOV     SI, [BP].SVM_Table  ; Get Saved Ptr to Mode Info
  574.     MOV     AX, [BP].SVM_Xsize  ; Get Display Width
  575.  
  576.     MOV     CX, AX              ; CX = Logical Width
  577.     SUB     CX, CS:[SI].M_XSize ; CX = Max X Scroll Value
  578.     MOV     MAX_XOFFSET, CX     ; Set Maximum X Scroll
  579.  
  580.     SHR     AX, 2               ; Bytes = Pixels / 4
  581.     MOV     SCREEN_WIDTH, AX    ; Save Width in Pixels
  582.  
  583.     SHR     AX, 1               ; Offset Value = Bytes / 2
  584.     MOV     AH, 13h             ; CRTC Offset Register Index
  585.     XCHG    AL, AH              ; Switch format for OUT
  586.     OUT     DX, AX              ; Set VGA CRTC Offset Reg
  587.  
  588.     ; Setup Data table, Y Scroll, Misc for Other Routines
  589.  
  590.     MOV     AX, [BP].SVM_Ysize  ; Get Logical Screen Height
  591.  
  592.     MOV     CX, AX              ; CX = Logical Height
  593.     SUB     BX, CS:[SI].M_YSize ; CX = Max Y Scroll Value
  594.     MOV     MAX_YOFFSET, CX     ; Set Maximum Y Scroll
  595.  
  596.     MOV     SCREEN_HEIGHT, AX   ; Save Height in Pixels
  597.     MUL     SCREEN_WIDTH        ; AX = Page Size in Bytes,
  598.     MOV     PAGE_SIZE, AX       ; Save Page Size
  599.  
  600.     MOV     CX, [BP].SVM_Pages  ; Get # of Pages
  601.     MOV     LAST_PAGE, CX       ; Save # of Pages
  602.  
  603.     CLR     BX                  ; Page # = 0
  604.     MOV     DX, BX              ; Page 0 Offset = 0
  605.  
  606. @SVM_Set_Pages:
  607.  
  608.     MOV     PAGE_ADDR[BX], DX   ; Set Page #(BX) Offset
  609.     ADD     BX, 2               ; Page#++
  610.     ADD     DX, AX              ; Compute Addr of Next Page
  611.     LOOPx   CX, @SVM_Set_Pages  ; Loop until all Pages Set
  612.  
  613.     ; Clear VGA Memory
  614.  
  615.     OUT_16  SC_INDEX, ALL_PLANES_ON ; Select All Planes
  616.     LES     DI, d CURRENT_PAGE      ; -> Start of VGA memory
  617.  
  618.     CLR     AX                  ; AX = 0
  619.     CLD                         ; Block Xfer Forwards
  620.     MOV     CX, 8000H           ; 32K * 4 * 2 = 256K
  621.     REP     STOSW               ; Clear dat memory!
  622.  
  623.     ; Setup Font Pointers
  624.  
  625.     MOV     BH, ROM_8x8_Lo      ; Ask for 8x8 Font, 0-127
  626.     MOV     AX, GET_CHAR_PTR    ; Service to Get Pointer
  627.     INT     10h                 ; Call VGA BIOS
  628.  
  629.     MOV     CHARSET_LOW, BP     ; Save Char Set Offset
  630.     MOV     CHARSET_LOW+2, ES   ; Save Char Set Segment
  631.  
  632.     MOV     BH, ROM_8x8_Hi      ; Ask for 8x8 Font, 128-255
  633.     MOV     AX, GET_CHAR_PTR    ; Service to Get Pointer
  634.     INT     10h                 ; Call VGA BIOS
  635.  
  636.     MOV     CHARSET_HI, BP      ; Save Char Set Offset
  637.     MOV     CHARSET_HI+2, ES    ; Save Char Set Segment
  638.  
  639.     MOV     AX, True            ; Return Success Code
  640.  
  641. @SVM_EXIT:
  642.     ADD     SP, 2               ; Deallocate workspace
  643.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  644.     RET     8                   ; Exit & Clean Up Stack
  645.  
  646. SET_VGA_MODEX   ENDP
  647.  
  648.  
  649. ;==================
  650. ;SET_MODEX% (Mode%)
  651. ;==================
  652. ;
  653. ; Quickie Mode Set - Sets Up Mode X to Default Configuration
  654. ;
  655. ; ENTRY: ModeType = Desired Screen Resolution (0-7)
  656. ;        (See SET_VGA_MODEX for list)
  657. ;
  658. ; EXIT:  AX = Success Flag:   0 = Failure / -1= Success
  659. ;
  660.  
  661. SM_STACK    STRUC
  662.                 DW  ?,? ; BP, SI
  663.                 DD  ?   ; Caller
  664.     SM_Mode     DW  ?   ; Desired Screen Resolution
  665. SM_STACK    ENDS
  666.  
  667.     PUBLIC  SET_MODEX
  668.  
  669. SET_MODEX   PROC    FAR
  670.  
  671.     PUSHx   BP, SI              ; Preserve Important registers
  672.     MOV     BP, SP              ; Set up Stack Frame
  673.  
  674.     CLR     AX                  ; Assume Failure
  675.     MOV     BX, [BP].SM_Mode    ; Get Desired Mode #
  676.     CMP     BX, NUM_MODES       ; Is it a Valid Mode #?
  677.     JAE     @SMX_Exit           ; If Not, don't Bother
  678.  
  679.     PUSH    BX                  ; Push Mode Parameter
  680.  
  681.     SHL     BX, 1                   ; Scale BX to word Index
  682.     MOV     SI, w MODE_TABLE[BX]    ; CS:SI -> Mode Info
  683.  
  684.     PUSH    CS:[SI].M_XSize     ; Push Default X Size
  685.     PUSH    CS:[SI].M_Ysize     ; Push Default Y size
  686.     MOV     AL, CS:[SI].M_Pages ; Get Default # of Pages
  687.     CLR     AH                  ; Hi Byte = 0
  688.     PUSH    AX                  ; Push # Pages
  689.  
  690.     CALL    f SET_VGA_MODEX     ; Set up Mode X!
  691.  
  692. @SMX_Exit:
  693.     POPx    SI, BP              ; Restore Registers
  694.     RET     2                   ; Exit & Clean Up Stack
  695.  
  696. SET_MODEX   ENDP
  697.  
  698.  
  699.     ; ===== BASIC GRAPHICS PRIMITIVES =====
  700.  
  701. ;============================
  702. ;CLEAR_VGA_SCREEN (ColorNum%)
  703. ;============================
  704. ;
  705. ; Clears the active display page
  706. ;
  707. ; ENTRY: ColorNum = Color Value to fill the page with
  708. ;
  709. ; EXIT:  No meaningful values returned
  710. ;
  711.  
  712. CVS_STACK   STRUC
  713.                 DW  ?,? ; DI, BP
  714.                 DD  ?   ; Caller
  715.     CVS_COLOR   DB  ?,? ; Color to Set Screen to
  716. CVS_STACK   ENDS
  717.  
  718.     PUBLIC  CLEAR_VGA_SCREEN
  719.  
  720. CLEAR_VGA_SCREEN    PROC    FAR
  721.  
  722.     PUSHx   BP, DI              ; Preserve Important Registers
  723.     MOV     BP, SP              ; Set up Stack Frame
  724.  
  725.     OUT_16  SC_INDEX, ALL_PLANES_ON ; Select All Planes
  726.     LES     DI, d CURRENT_PAGE      ; Point to Active VGA Page
  727.  
  728.     MOV     AL, [BP].CVS_COLOR  ; Get Color
  729.     MOV     AH, AL              ; Copy for Word Write
  730.     CLD                         ; Block fill Forwards
  731.  
  732.     MOV     CX, PAGE_SIZE       ; Get Size of Page
  733.     SHR     CX, 1               ; Divide by 2 for Words
  734.     REP     STOSW               ; Block Fill VGA memory
  735.  
  736.     POPx    DI, BP              ; Restore Saved Registers
  737.     RET     2                   ; Exit & Clean Up Stack
  738.  
  739. CLEAR_VGA_SCREEN    ENDP
  740.  
  741.  
  742. ;===================================
  743. ;SET_POINT (Xpos%, Ypos%, ColorNum%)
  744. ;===================================
  745. ;
  746. ; Plots a single Pixel on the active display page
  747. ;
  748. ; ENTRY: Xpos     = X position to plot pixel at
  749. ;        Ypos     = Y position to plot pixel at
  750. ;        ColorNum = Color to plot pixel with
  751. ;
  752. ; EXIT:  No meaningful values returned
  753. ;
  754.  
  755. SP_STACK    STRUC
  756.                 DW  ?,? ; BP, DI
  757.                 DD  ?   ; Caller
  758.     SETP_Color  DB  ?,? ; Color of Point to Plot
  759.     SETP_Ypos   DW  ?   ; Y pos of Point to Plot
  760.     SETP_Xpos   DW  ?   ; X pos of Point to Plot
  761. SP_STACK    ENDS
  762.  
  763.         PUBLIC SET_POINT
  764.  
  765. SET_POINT   PROC    FAR
  766.  
  767.     PUSHx   BP, DI              ; Preserve Registers
  768.     MOV     BP, SP              ; Set up Stack Frame
  769.  
  770.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  771.  
  772.     MOV     AX, [BP].SETP_Ypos  ; Get Line # of Pixel
  773.     MUL     SCREEN_WIDTH        ; Get Offset to Start of Line
  774.  
  775.     MOV     BX, [BP].SETP_Xpos  ; Get Xpos
  776.     MOV     CX, BX              ; Copy to extract Plane # from
  777.     SHR     BX, 2               ; X offset (Bytes) = Xpos/4
  778.     ADD     BX, AX              ; Offset = Width*Ypos + Xpos/4
  779.  
  780.     MOV     AX, MAP_MASK_PLANE1 ; Map Mask & Plane Select Register
  781.     AND     CL, PLANE_BITS      ; Get Plane Bits
  782.     SHL     AH, CL              ; Get Plane Select Value
  783.     OUT_16  SC_Index, AX        ; Select Plane
  784.  
  785.     MOV     AL,[BP].SETP_Color  ; Get Pixel Color
  786.     MOV     ES:[DI+BX], AL      ; Draw Pixel
  787.  
  788.     POPx    DI, BP              ; Restore Saved Registers
  789.     RET     6                   ; Exit and Clean up Stack
  790.  
  791. SET_POINT        ENDP
  792.  
  793.  
  794. ;==========================
  795. ;READ_POINT% (Xpos%, Ypos%)
  796. ;==========================
  797. ;
  798. ; Read the color of a pixel from the Active Display Page
  799. ;
  800. ; ENTRY: Xpos = X position of pixel to read
  801. ;        Ypos = Y position of pixel to read
  802. ;
  803. ; EXIT:  AX   = Color of Pixel at (Xpos, Ypos)
  804. ;
  805.  
  806. RP_STACK    STRUC
  807.             DW  ?,? ; BP, DI
  808.             DD  ?   ; Caller
  809.     RP_Ypos DW  ?   ; Y pos of Point to Read
  810.     RP_Xpos DW  ?   ; X pos of Point to Read
  811. RP_STACK    ENDS
  812.  
  813.         PUBLIC  READ_POINT
  814.  
  815. READ_POINT      PROC    FAR
  816.  
  817.     PUSHx   BP, DI              ; Preserve Registers
  818.     MOV     BP, SP              ; Set up Stack Frame
  819.  
  820.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  821.  
  822.     MOV     AX, [BP].RP_Ypos    ; Get Line # of Pixel
  823.     MUL     SCREEN_WIDTH        ; Get Offset to Start of Line
  824.  
  825.     MOV     BX, [BP].RP_Xpos    ; Get Xpos
  826.     MOV     CX, BX
  827.     SHR     BX, 2               ; X offset (Bytes) = Xpos/4
  828.     ADD     BX, AX              ; Offset = Width*Ypos + Xpos/4
  829.  
  830.     MOV     AL, READ_MAP        ; GC Read Mask Register
  831.     MOV     AH, CL              ; Get Xpos
  832.     AND     AH, PLANE_BITS      ; & mask out Plane #
  833.     OUT_16  GC_INDEX, AX        ; Select Plane to read in
  834.  
  835.     CLR     AH                  ; Clear Return Value Hi byte
  836.     MOV     AL, ES:[DI+BX]      ; Get Color of Pixel
  837.  
  838.     POPx    DI, BP              ; Restore Saved Registers
  839.     RET     4                   ; Exit and Clean up Stack
  840.  
  841. READ_POINT        ENDP
  842.  
  843.  
  844. ;======================================================
  845. ;FILL_BLOCK (Xpos1%, Ypos1%, Xpos2%, Ypos2%, ColorNum%)
  846. ;======================================================
  847. ;
  848. ; Fills a rectangular block on the active display Page
  849. ;
  850. ; ENTRY: Xpos1    = Left X position of area to fill
  851. ;        Ypos1    = Top Y position of area to fill
  852. ;        Xpos2    = Right X position of area to fill
  853. ;        Ypos2    = Bottom Y position of area to fill
  854. ;        ColorNum = Color to fill area with
  855. ;
  856. ; EXIT:  No meaningful values returned
  857. ;
  858.  
  859. FB_STACK    STRUC
  860.                 DW  ?x4 ; DS, DI, SI, BP
  861.                 DD  ?   ; Caller
  862.     FB_Color    DB  ?,? ; Fill Color
  863.     FB_Ypos2    DW  ?   ; Y pos of Lower Right Pixel
  864.     FB_Xpos2    DW  ?   ; X pos of Lower Right Pixel
  865.     FB_Ypos1    DW  ?   ; Y pos of Upper Left Pixel
  866.     FB_Xpos1    DW  ?   ; X pos of Upper Left Pixel
  867. FB_STACK    ENDS
  868.  
  869.         PUBLIC    FILL_BLOCK
  870.  
  871. FILL_BLOCK  PROC    FAR
  872.  
  873.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  874.     MOV     BP, SP              ; Set up Stack Frame
  875.  
  876.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  877.     CLD                         ; Direction Flag = Forward
  878.  
  879.     OUT_8   SC_INDEX, MAP_MASK  ; Set up for Plane Select
  880.  
  881.     ; Validate Pixel Coordinates
  882.     ; If necessary, Swap so X1 <= X2, Y1 <= Y2
  883.  
  884.     MOV     AX, [BP].FB_Ypos1   ; AX = Y1   is Y1< Y2?
  885.     MOV     BX, [BP].FB_Ypos2   ; BX = Y2
  886.     CMP     AX, BX
  887.     JLE     @FB_NOSWAP1
  888.  
  889.     MOV     [BP].FB_Ypos1, BX   ; Swap Y1 and Y2 and save Y1
  890.     XCHG    AX, BX              ; on stack for future use
  891.  
  892. @FB_NOSWAP1:
  893.     SUB     BX, AX              ; Get Y width
  894.     INC     BX                  ; Add 1 to avoid 0 value
  895.     MOV     [BP].FB_Ypos2, BX   ; Save in Ypos2
  896.  
  897.     MUL     SCREEN_WIDTH        ; Mul Y1 by Bytes per Line
  898.     ADD     DI, AX              ; DI = Start of Line Y1
  899.  
  900.     MOV     AX, [BP].FB_Xpos1   ; Check X1 <= X2
  901.     MOV     BX, [BP].FB_Xpos2   ;
  902.     CMP     AX, BX
  903.     JLE     @FB_NOSWAP2         ; Skip Ahead if Ok
  904.  
  905.     MOV     [BP].FB_Xpos2, AX   ; Swap X1 AND X2 and save X2
  906.     XCHG    AX, BX              ; on stack for future use
  907.  
  908.     ; All our Input Values are in order, Now determine
  909.     ; How many full "bands" 4 pixels wide (aligned) there
  910.     ; are, and if there are partial bands (<4 pixels) on
  911.     ; the left and right edges.
  912.  
  913. @FB_NOSWAP2:
  914.     MOV     DX, AX              ; DX = X1 (Pixel Position)
  915.     SHR     DX, 2               ; DX/4 = Bytes into Line
  916.     ADD     DI, DX              ; DI = Addr of Upper-Left Corner
  917.  
  918.     MOV     CX, BX              ; CX = X2 (Pixel Position)
  919.     SHR     CX, 2               ; CX/4 = Bytes into Line
  920.  
  921.     CMP     DX, CX              ; Start and end in same band?
  922.     JNE     @FB_NORMAL          ; if not, check for l & r edges
  923.     JMP     @FB_ONE_BAND_ONLY   ; if so, then special processing
  924.  
  925. @FB_NORMAL:
  926.     SUB     CX, DX              ; CX = # bands -1
  927.     MOV     SI, AX              ; SI = PLANE#(X1)
  928.     AND     SI, PLANE_BITS      ; if Left edge is aligned then
  929.     JZ      @FB_L_PLANE_FLUSH   ; no special processing..
  930.  
  931.     ; Draw "Left Edge" vertical strip of 1-3 pixels...
  932.  
  933.     OUT_8   SC_Data, Left_Clip_Mask[SI] ; Set Left Edge Plane Mask
  934.  
  935.     MOV     SI, DI              ; SI = Copy of Start Addr (UL)
  936.  
  937.     MOV     DX, [BP].FB_Ypos2   ; Get # of Lines to draw
  938.     MOV     AL, [BP].FB_Color   ; Get Fill Color
  939.     MOV     BX, SCREEN_WIDTH    ; Get Vertical increment Value
  940.  
  941. @FB_LEFT_LOOP:
  942.     MOV     ES:[SI], AL         ; Fill in Left Edge Pixels
  943.     ADD     SI, BX              ; Point to Next Line (Below)
  944.     LOOPjz  DX, @FB_LEFT_CONT   ; Exit loop if all Lines Drawn
  945.  
  946.     MOV     ES:[SI], AL         ; Fill in Left Edge Pixels
  947.     ADD     SI, BX              ; Point to Next Line (Below)
  948.     LOOPx   DX, @FB_LEFT_LOOP   ; loop until left strip is drawn
  949.  
  950. @FB_LEFT_CONT:
  951.  
  952.     INC     DI                  ; Point to Middle (or Right) Block
  953.     DEC     CX                  ; Reset CX instead of JMP @FB_RIGHT
  954.  
  955. @FB_L_PLANE_FLUSH:
  956.     INC     CX                  ; Add in Left band to middle block
  957.  
  958.     ; DI = Addr of 1st middle Pixel (band) to fill
  959.     ; CX = # of Bands to fill -1
  960.  
  961. @FB_RIGHT:
  962.     MOV     SI, [BP].FB_Xpos2   ; Get Xpos2
  963.     AND     SI, PLANE_BITS      ; Get Plane values
  964.     CMP     SI, 0003            ; Plane = 3?
  965.     JE      @FB_R_EDGE_FLUSH    ; Hey, add to middle
  966.  
  967.     ; Draw "Right Edge" vertical strip of 1-3 pixels...
  968.  
  969.     OUT_8   SC_Data, Right_Clip_Mask[SI]    ; Right Edge Plane Mask
  970.  
  971.     MOV     SI, DI              ; Get Addr of Left Edge
  972.     ADD     SI, CX              ; Add Width-1 (Bands)
  973.     DEC     SI                  ; To point to top of Right Edge
  974.  
  975.     MOV     DX, [BP].FB_Ypos2   ; Get # of Lines to draw
  976.     MOV     AL, [BP].FB_Color   ; Get Fill Color
  977.     MOV     BX, SCREEN_WIDTH    ; Get Vertical increment Value
  978.  
  979. @FB_RIGHT_LOOP:
  980.     MOV     ES:[SI], AL         ; Fill in Right Edge Pixels
  981.     ADD     SI, BX              ; Point to Next Line (Below)
  982.     LOOPjz  DX, @FB_RIGHT_CONT  ; Exit loop if all Lines Drawn
  983.  
  984.     MOV     ES:[SI], AL         ; Fill in Right Edge Pixels
  985.     ADD     SI, BX              ; Point to Next Line (Below)
  986.     LOOPx   DX, @FB_RIGHT_LOOP  ; loop until left strip is drawn
  987.  
  988. @FB_RIGHT_CONT:
  989.  
  990.     DEC     CX                  ; Minus 1 for Middle bands
  991.     JZ      @FB_EXIT            ; Uh.. no Middle bands...
  992.  
  993. @FB_R_EDGE_FLUSH:
  994.  
  995.     ; DI = Addr of Upper Left block to fill
  996.     ; CX = # of Bands to fill in (width)
  997.  
  998.     OUT_8   SC_Data, ALL_PLANES ; Write to All Planes
  999.  
  1000.     MOV     DX, SCREEN_WIDTH    ; DX = DI Increment
  1001.     SUB     DX, CX              ;  = Screen_Width-# Planes Filled
  1002.  
  1003.     MOV     BX, CX              ; BX = Quick Refill for CX
  1004.     MOV     SI, [BP].FB_Ypos2   ; SI = # of Line to Fill
  1005.     MOV     AL, [BP].FB_Color   ; Get Fill Color
  1006.  
  1007. @FB_MIDDLE_LOOP:
  1008.     REP     STOSB               ; Fill in entire line
  1009.  
  1010.     MOV     CX, BX              ; Recharge CX (Line Width)
  1011.     ADD     DI, DX              ; Point to start of Next Line
  1012.     LOOPx   SI, @FB_MIDDLE_LOOP ; Loop until all lines drawn
  1013.  
  1014.     JMP     s @FB_EXIT          ; Outa here
  1015.  
  1016. @FB_ONE_BAND_ONLY:
  1017.     MOV     SI, AX                  ; Get Left Clip Mask, Save X1
  1018.     AND     SI, PLANE_BITS          ; Mask out Row #
  1019.     MOV     AL, Left_Clip_Mask[SI]  ; Get Left Edge Mask
  1020.     MOV     SI, BX                  ; Get Right Clip Mask, Save X2
  1021.     AND     SI, PLANE_BITS          ; Mask out Row #
  1022.     AND     AL, Right_Clip_Mask[SI] ; Get Right Edge Mask byte
  1023.  
  1024.     OUT_8   SC_Data, AL         ; Clip For Left & Right Masks
  1025.  
  1026.     MOV     CX, [BP].FB_Ypos2   ; Get # of Lines to draw
  1027.     MOV     AL, [BP].FB_Color   ; Get Fill Color
  1028.     MOV     BX, SCREEN_WIDTH    ; Get Vertical increment Value
  1029.  
  1030. @FB_ONE_LOOP:
  1031.     MOV     ES:[DI], AL         ; Fill in Pixels
  1032.     ADD     DI, BX              ; Point to Next Line (Below)
  1033.     LOOPjz  CX, @FB_EXIT        ; Exit loop if all Lines Drawn
  1034.  
  1035.     MOV     ES:[DI], AL         ; Fill in Pixels
  1036.     ADD     DI, BX              ; Point to Next Line (Below)
  1037.     LOOPx   CX, @FB_ONE_LOOP    ; loop until left strip is drawn
  1038.  
  1039. @FB_EXIT:
  1040.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  1041.     RET     10                  ; Exit and Clean up Stack
  1042.  
  1043. FILL_BLOCK   ENDP
  1044.  
  1045.  
  1046. ;=====================================================
  1047. ;DRAW_LINE (Xpos1%, Ypos1%, Xpos2%, Ypos2%, ColorNum%)
  1048. ;=====================================================
  1049. ;
  1050. ; Draws a Line on the active display page
  1051. ;
  1052. ; ENTRY: Xpos1    = X position of first point on line
  1053. ;        Ypos1    = Y position of first point on line
  1054. ;        Xpos2    = X position of last point on line
  1055. ;        Ypos2    = Y position of last point on line
  1056. ;        ColorNum = Color to draw line with
  1057. ;
  1058. ; EXIT:  No meaningful values returned
  1059. ;
  1060.  
  1061. DL_STACK    STRUC
  1062.                 DW  ?x3 ; DI, SI, BP
  1063.                 DD  ?   ; Caller
  1064.     DL_ColorF   DB  ?,? ; Line Draw Color
  1065.     DL_Ypos2    DW  ?   ; Y pos of last point
  1066.     DL_Xpos2    DW  ?   ; X pos of last point
  1067.     DL_Ypos1    DW  ?   ; Y pos of first point
  1068.     DL_Xpos1    DW  ?   ; X pos of first point
  1069. DL_STACK    ENDS
  1070.  
  1071.         PUBLIC DRAW_LINE
  1072.  
  1073. DRAW_LINE   PROC    FAR
  1074.  
  1075.     PUSHx   BP, SI, DI          ; Preserve Important Registers
  1076.     MOV     BP, SP              ; Set up Stack Frame
  1077.     CLD                         ; Direction Flag = Forward
  1078.  
  1079.     OUT_8   SC_INDEX, MAP_MASK  ; Set up for Plane Select
  1080.     MOV     CH, [BP].DL_ColorF  ; Save Line Color in CH
  1081.  
  1082.     ; Check Line Type
  1083.  
  1084.     MOV     SI, [BP].DL_Xpos1   ; AX = X1   is X1< X2?
  1085.     MOV     DI, [BP].DL_Xpos2   ; DX = X2
  1086.     CMP     SI, DI              ; Is X1 < X2
  1087.     JE      @DL_VLINE           ; If X1=X2, Draw Vertical Line
  1088.     JL      @DL_NOSWAP1         ; If X1 < X2, don't swap
  1089.  
  1090.     XCHG    SI, DI              ; X2 IS > X1, SO SWAP THEM
  1091.  
  1092. @DL_NOSWAP1:
  1093.  
  1094.     ; SI = X1, DI = X2
  1095.  
  1096.     MOV     AX, [BP].DL_Ypos1   ; AX = Y1   is Y1 <> Y2?
  1097.     CMP     AX, [BP].DL_Ypos2   ; Y1 = Y2?
  1098.     JE      @DL_HORZ            ; If so, Draw a Horizontal Line
  1099.  
  1100.     JMP     @DL_BREZHAM         ; Diagonal line... go do it...
  1101.  
  1102.     ; This Code draws a Horizontal Line in Mode X where:
  1103.     ; SI = X1, DI = X2, and AX = Y1/Y2
  1104.  
  1105. @DL_HORZ:
  1106.  
  1107.     MUL     SCREEN_WIDTH        ; Offset = Ypos * Screen_Width
  1108.     MOV     DX, AX              ; CX = Line offset into Page
  1109.  
  1110.     MOV     AX, SI                  ; Get Left edge, Save X1
  1111.     AND     SI, PLANE_BITS          ; Mask out Row #
  1112.     MOV     BL, Left_Clip_Mask[SI]  ; Get Left Edge Mask
  1113.     MOV     CX, DI                  ; Get Right edge, Save X2
  1114.     AND     DI, PLANE_BITS          ; Mask out Row #
  1115.     MOV     BH, Right_Clip_Mask[DI] ; Get Right Edge Mask byte
  1116.  
  1117.     SHR     AX, 2               ; Get X1 Byte # (=X1/4)
  1118.     SHR     CX, 2               ; Get X2 Byte # (=X2/4)
  1119.  
  1120.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  1121.     ADD     DI, DX              ; Point to Start of Line
  1122.     ADD     DI, AX              ; Point to Pixel X1
  1123.  
  1124.     SUB     CX, AX              ; CX = # Of Bands (-1) to set
  1125.     JNZ     @DL_LONGLN          ; jump if longer than one segment
  1126.  
  1127.     AND     BL, BH              ; otherwise, merge clip masks
  1128.  
  1129. @DL_LONGLN:
  1130.  
  1131.     OUT_8   SC_Data, BL         ; Set the Left Clip Mask
  1132.  
  1133.     MOV     AL, [BP].DL_ColorF  ; Get Line Color
  1134.     MOV     BL, AL              ; BL = Copy of Line Color
  1135.     STOSB                       ; Set Left (1-4) Pixels
  1136.  
  1137.     JCXZ    @DL_EXIT            ; Done if only one Line Segment
  1138.  
  1139.     DEC     CX                  ; CX = # of Middle Segments
  1140.     JZ      @DL_XRSEG           ; If no middle segments....
  1141.  
  1142.     ; Draw Middle Segments
  1143.  
  1144.     OUT_8   DX, ALL_PLANES      ; Write to ALL Planes
  1145.  
  1146.     MOV     AL, BL              ; Get Color from BL
  1147.     REP     STOSB               ; Draw Middle (4 Pixel) Segments
  1148.  
  1149. @DL_XRSEG:
  1150.     OUT_8   DX, BH              ; Select Planes for Right Clip Mask
  1151.     MOV     AL, BL              ; Get Color Value
  1152.     STOSB                       ; Draw Right (1-4) Pixels
  1153.  
  1154.     JMP     s @DL_EXIT          ; We Are Done...
  1155.  
  1156.  
  1157.     ; This Code Draws A Vertical Line.  On entry:
  1158.     ; CH = Line Color, SI & DI = X1
  1159.  
  1160. @DL_VLINE:
  1161.  
  1162.     MOV     AX, [BP].DL_Ypos1   ; AX = Y1
  1163.     MOV     SI, [BP].DL_Ypos2   ; SI = Y2
  1164.     CMP     AX, SI              ; Is Y1 < Y2?
  1165.     JLE     @DL_NOSWAP2         ; if so, Don't Swap them
  1166.  
  1167.     XCHG    AX, SI              ; Ok, NOW Y1 < Y2
  1168.  
  1169. @DL_NOSWAP2:
  1170.  
  1171.     SUB     SI, AX              ; SI = Line Height (Y2-Y1+1)
  1172.     INC     SI
  1173.  
  1174.     ; AX = Y1, DI = X1, Get offset into Page into AX
  1175.  
  1176.     MUL     SCREEN_WIDTH        ; Offset = Y1 (AX) * Screen Width
  1177.     MOV     DX, DI              ; Copy Xpos into DX
  1178.     SHR     DI, 2               ; DI = Xpos/4
  1179.     ADD     AX, DI              ; DI = Xpos/4 + ScreenWidth * Y1
  1180.  
  1181.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  1182.     ADD     DI, AX              ; Point to Pixel X1, Y1
  1183.  
  1184.     ;Select Plane
  1185.  
  1186.     MOV     CL, DL              ; CL = Save X1
  1187.     AND     CL, PLANE_BITS      ; Get X1 MOD 4 (Plane #)
  1188.     MOV     AX, MAP_MASK_PLANE1 ; Code to set Plane #1
  1189.     SHL     AH, CL              ; Change to Correct Plane #
  1190.     OUT_16  SC_Index, AX        ; Select Plane
  1191.  
  1192.     MOV     AL, CH              ; Get Saved Color
  1193.     MOV     BX, SCREEN_WIDTH    ; Get Offset to Advance Line By
  1194.  
  1195. @DL_VLoop:
  1196.     MOV     ES:[DI], AL         ; Draw Single Pixel
  1197.     ADD     DI, BX              ; Point to Next Line
  1198.     LOOPjz  SI, @DL_EXIT        ; Lines--, Exit if done
  1199.  
  1200.     MOV     ES:[DI], AL         ; Draw Single Pixel
  1201.     ADD     DI, BX              ; Point to Next Line
  1202.     LOOPx   SI, @DL_VLoop       ; Lines--, Loop until Done
  1203.  
  1204. @DL_EXIT:
  1205.  
  1206.     JMP     @DL_EXIT2           ; Done!
  1207.  
  1208.     ; This code Draws a diagonal line in Mode X
  1209.  
  1210. @DL_BREZHAM:
  1211.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  1212.  
  1213.     MOV     AX, [BP].DL_Ypos1   ; get Y1 value
  1214.     MOV     BX, [BP].DL_Ypos2   ; get Y2 value
  1215.     MOV     CX, [BP].DL_Xpos1   ; Get Starting Xpos
  1216.  
  1217.     CMP     BX, AX              ; Y2-Y1 is?
  1218.     JNC     @DL_DeltaYOK        ; if Y2>=Y1 then goto...
  1219.  
  1220.     XCHG    BX, AX              ; Swap em...
  1221.     MOV     CX, [BP].DL_Xpos2   ; Get New Starting Xpos
  1222.  
  1223. @DL_DeltaYOK:
  1224.     MUL     SCREEN_WIDTH        ; Offset = SCREEN_WIDTH * Y1
  1225.  
  1226.     ADD     DI, AX              ; DI -> Start of Line Y1 on Page
  1227.     MOV     AX, CX              ; AX = Xpos (X1)
  1228.     SHR     AX, 2               ; /4 = Byte Offset into Line
  1229.     ADD     DI, AX              ; DI = Starting pos (X1,Y1)
  1230.  
  1231.     MOV     AL, 11h             ; Staring Mask
  1232.     AND     CL, PLANE_BITS      ; Get Plane #
  1233.     SHL     AL, CL              ; and shift into place
  1234.     MOV     AH, [BP].DL_ColorF  ; Color in Hi Bytes
  1235.  
  1236.     PUSH    AX                  ; Save Mask,Color...
  1237.  
  1238.     MOV     AH, AL              ; Plane # in AH
  1239.     MOV     AL, MAP_MASK        ; Select Plane Register
  1240.     OUT_16  SC_Index, AX        ; Select initial plane
  1241.  
  1242.     MOV     AX, [BP].DL_Xpos1   ; get X1 value
  1243.     MOV     BX, [BP].DL_Ypos1   ; get Y1 value
  1244.     MOV     CX, [BP].DL_Xpos2   ; get X2 value
  1245.     MOV     DX, [BP].DL_Ypos2   ; get Y2 value
  1246.  
  1247.     MOV     BP, SCREEN_WIDTH    ; Use BP for Line width to
  1248.                                 ; to avoid extra memory access
  1249.  
  1250.     SUB     DX, BX              ; figure Delta_Y
  1251.     JNC     @DL_DeltaYOK2       ; jump if Y2 >= Y1
  1252.  
  1253.     ADD     BX, DX              ; put Y2 into Y1
  1254.     NEG     DX                  ; abs(Delta_Y)
  1255.     XCHG    AX, CX              ; and exchange X1 and X2
  1256.  
  1257. @DL_DeltaYOK2:
  1258.     MOV     BX, 08000H          ; seed for fraction accumulator
  1259.  
  1260.     SUB     CX, AX              ; figure Delta_X
  1261.     JC      @DL_DrawLeft        ; if negative, go left
  1262.  
  1263.     JMP     @DL_DrawRight       ; Draw Line that slopes right
  1264.  
  1265. @DL_DrawLeft:
  1266.  
  1267.     NEG     CX                  ; abs(Delta_X)
  1268.  
  1269.     CMP     CX, DX              ; is Delta_X < Delta_Y?
  1270.     JB      @DL_SteepLeft       ; yes, so go do steep line
  1271.                                 ; (Delta_Y iterations)
  1272.  
  1273.     ; Draw a Shallow line to the left in Mode X
  1274.  
  1275. @DL_ShallowLeft:
  1276.     CLR     AX                  ; zero low word of Delta_Y * 10000h
  1277.     SUB     AX, DX              ; DX:AX <- DX * 0FFFFh
  1278.     SBB     DX, 0               ; include carry
  1279.     DIV     CX                  ; divide by Delta_X
  1280.  
  1281.     MOV     SI, BX              ; SI = Accumulator
  1282.     MOV     BX, AX              ; BX = Add fraction
  1283.     POP     AX                  ; Get Color, Bit mask
  1284.     MOV     DX, SC_Data         ; Sequence controller data register
  1285.     INC     CX                  ; Inc Delta_X so we can unroll loop
  1286.  
  1287.     ; Loop (x2) to Draw Pixels, Move Left, and Maybe Down...
  1288.  
  1289. @DL_SLLLoop:
  1290.     MOV     ES:[DI], AH         ; set first pixel, plane data set up
  1291.     LOOPjz  CX, @DL_SLLExit     ; Delta_X--, Exit if done
  1292.  
  1293.     ADD     SI, BX              ; add numerator to accumulator
  1294.     JNC     @DL_SLLL2nc         ; move down on carry
  1295.  
  1296.     ADD     DI, BP              ; Move Down one line...
  1297.  
  1298. @DL_SLLL2nc:
  1299.     DEC     DI                  ; Left one addr
  1300.     ROR     AL, 1               ; Move Left one plane, back on 0 1 2
  1301.     CMP     AL, 87h             ; wrap?, if AL <88 then Carry set
  1302.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1303.     OUT     DX, AL              ; Set up New Bit Plane mask
  1304.  
  1305.     MOV     ES:[DI], AH         ; set pixel
  1306.     LOOPjz  CX, @DL_SLLExit     ; Delta_X--, Exit if done
  1307.  
  1308.     ADD     SI, BX              ; add numerator to accumulator,
  1309.     JNC     @DL_SLLL3nc         ; move down on carry
  1310.  
  1311.     ADD     DI, BP              ; Move Down one line...
  1312.  
  1313. @DL_SLLL3nc:                    ; Now move left a pixel...
  1314.     DEC     DI                  ; Left one addr
  1315.     ROR     AL, 1               ; Move Left one plane, back on 0 1 2
  1316.     CMP     AL, 87h             ; Wrap?, if AL <88 then Carry set
  1317.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1318.     OUT     DX, AL              ; Set up New Bit Plane mask
  1319.     JMP     s @DL_SLLLoop       ; loop until done
  1320.  
  1321. @DL_SLLExit:
  1322.     JMP     @DL_EXIT2           ; and exit
  1323.  
  1324.     ; Draw a steep line to the left in Mode X
  1325.  
  1326. @DL_SteepLeft:
  1327.     CLR     AX                  ; zero low word of Delta_Y * 10000h
  1328.     XCHG    DX, CX              ; Delta_Y switched with Delta_X
  1329.     DIV     CX                  ; divide by Delta_Y
  1330.  
  1331.     MOV     SI, BX              ; SI = Accumulator
  1332.     MOV     BX, AX              ; BX = Add Fraction
  1333.     POP     AX                  ; Get Color, Bit mask
  1334.     MOV     DX, SC_Data         ; Sequence controller data register
  1335.     INC     CX                  ; Inc Delta_Y so we can unroll loop
  1336.  
  1337.     ; Loop (x2) to Draw Pixels, Move Down, and Maybe left
  1338.  
  1339. @DL_STLLoop:
  1340.  
  1341.     MOV     ES:[DI], AH         ; set first pixel
  1342.     LOOPjz  CX, @DL_STLExit     ; Delta_Y--, Exit if done
  1343.  
  1344.     ADD     SI, BX              ; add numerator to accumulator
  1345.     JNC     @DL_STLnc2          ; No carry, just move down!
  1346.  
  1347.     DEC     DI                  ; Move Left one addr
  1348.     ROR     AL, 1               ; Move Left one plane, back on 0 1 2
  1349.     CMP     AL, 87h             ; Wrap?, if AL <88 then Carry set
  1350.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1351.     OUT     DX, AL              ; Set up New Bit Plane mask
  1352.  
  1353. @DL_STLnc2:
  1354.     ADD     DI, BP              ; advance to next line.
  1355.  
  1356.     MOV     ES:[DI], AH         ; set pixel
  1357.     LOOPjz  CX, @DL_STLExit     ; Delta_Y--, Exit if done
  1358.  
  1359.     ADD     SI, BX              ; add numerator to accumulator
  1360.     JNC     @DL_STLnc3          ; No carry, just move down!
  1361.  
  1362.     DEC     DI                  ; Move Left one addr
  1363.     ROR     AL, 1               ; Move Left one plane, back on 0 1 2
  1364.     CMP     AL, 87h             ; Wrap?, if AL <88 then Carry set
  1365.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1366.     OUT     DX, AL              ; Set up New Bit Plane mask
  1367.  
  1368. @DL_STLnc3:
  1369.     ADD     DI, BP              ; advance to next line.
  1370.     JMP     s @DL_STLLoop       ; Loop until done
  1371.  
  1372. @DL_STLExit:
  1373.     JMP     @DL_EXIT2           ; and exit
  1374.  
  1375.     ; Draw a line that goes to the Right...
  1376.  
  1377. @DL_DrawRight:
  1378.     CMP     CX, DX              ; is Delta_X < Delta_Y?
  1379.     JB      @DL_SteepRight      ; yes, so go do steep line
  1380.                                 ; (Delta_Y iterations)
  1381.  
  1382.     ; Draw a Shallow line to the Right in Mode X
  1383.  
  1384. @DL_ShallowRight:
  1385.     CLR     AX                  ; zero low word of Delta_Y * 10000h
  1386.     SUB     AX, DX              ; DX:AX <- DX * 0FFFFh
  1387.     SBB     DX, 0               ; include carry
  1388.     DIV     CX                  ; divide by Delta_X
  1389.  
  1390.     MOV     SI, BX              ; SI = Accumulator
  1391.     MOV     BX, AX              ; BX = Add Fraction
  1392.     POP     AX                  ; Get Color, Bit mask
  1393.     MOV     DX, SC_Data         ; Sequence controller data register
  1394.     INC     CX                  ; Inc Delta_X so we can unroll loop
  1395.  
  1396.     ; Loop (x2) to Draw Pixels, Move Right, and Maybe Down...
  1397.  
  1398. @DL_SLRLoop:
  1399.     MOV     ES:[DI], AH         ; set first pixel, mask is set up
  1400.     LOOPjz  CX, @DL_SLRExit     ; Delta_X--, Exit if done..
  1401.  
  1402.     ADD     SI, BX              ; add numerator to accumulator
  1403.     JNC     @DL_SLR2nc          ; don't move down if carry not set
  1404.  
  1405.     ADD     DI, BP              ; Move Down one line...
  1406.  
  1407. @DL_SLR2nc:                     ; Now move right a pixel...
  1408.     ROL     AL, 1               ; Move Right one addr if Plane = 0
  1409.     CMP     AL, 12h             ; Wrap? if AL >12 then Carry not set
  1410.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1411.     OUT     DX, AL              ; Set up New Bit Plane mask
  1412.  
  1413.     MOV     ES:[DI], AH         ; set pixel
  1414.     LOOPjz  CX, @DL_SLRExit     ; Delta_X--, Exit if done..
  1415.  
  1416.     ADD     SI, BX              ; add numerator to accumulator
  1417.     JNC     @DL_SLR3nc          ; don't move down if carry not set
  1418.  
  1419.     ADD     DI, BP              ; Move Down one line...
  1420.  
  1421. @DL_SLR3nc:
  1422.     ROL     AL, 1               ; Move Right one addr if Plane = 0
  1423.     CMP     AL, 12h             ; Wrap? if AL >12 then Carry not set
  1424.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1425.     OUT     DX, AL              ; Set up New Bit Plane mask
  1426.     JMP     s @DL_SLRLoop       ; loop till done
  1427.  
  1428. @DL_SLRExit:
  1429.     JMP     @DL_EXIT2           ; and exit
  1430.  
  1431.     ; Draw a Steep line to the Right in Mode X
  1432.  
  1433. @DL_SteepRight:
  1434.     CLR     AX                  ; zero low word of Delta_Y * 10000h
  1435.     XCHG    DX, CX              ; Delta_Y switched with Delta_X
  1436.     DIV     CX                  ; divide by Delta_Y
  1437.  
  1438.     MOV     SI, BX              ; SI = Accumulator
  1439.     MOV     BX, AX              ; BX = Add Fraction
  1440.     POP     AX                  ; Get Color, Bit mask
  1441.     MOV     DX, SC_Data         ; Sequence controller data register
  1442.     INC     CX                  ; Inc Delta_Y so we can unroll loop
  1443.  
  1444.     ; Loop (x2) to Draw Pixels, Move Down, and Maybe Right
  1445.  
  1446. @STRLoop:
  1447.     MOV     ES:[DI], AH         ; set first pixel, mask is set up
  1448.     LOOPjz  CX, @DL_EXIT2       ; Delta_Y--, Exit if Done
  1449.  
  1450.     ADD     SI, BX              ; add numerator to accumulator
  1451.     JNC     @STRnc2             ; if no carry then just go down...
  1452.  
  1453.     ROL     AL, 1               ; Move Right one addr if Plane = 0
  1454.     CMP     AL, 12h             ; Wrap? if AL >12 then Carry not set
  1455.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1456.     OUT     DX, AL              ; Set up New Bit Plane mask
  1457.  
  1458. @STRnc2:
  1459.     ADD     DI, BP              ; advance to next line.
  1460.  
  1461.     MOV     ES:[DI], AH         ; set pixel
  1462.     LOOPjz  CX, @DL_EXIT2       ; Delta_Y--, Exit if Done
  1463.  
  1464.     ADD     SI, BX              ; add numerator to accumulator
  1465.     JNC     @STRnc3             ; if no carry then just go down...
  1466.  
  1467.     ROL     AL, 1               ; Move Right one addr if Plane = 0
  1468.     CMP     AL, 12h             ; Wrap? if AL >12 then Carry not set
  1469.     ADC     DI, 0               ; Adjust Address: DI = DI + Carry
  1470.     OUT     DX, AL              ; Set up New Bit Plane mask
  1471.  
  1472. @STRnc3:
  1473.     ADD     DI, BP              ; advance to next line.
  1474.     JMP     s @STRLoop          ; loop till done
  1475.  
  1476. @DL_EXIT2:
  1477.     POPx    DI, SI, BP          ; Restore Saved Registers
  1478.     RET     10                  ; Exit and Clean up Stack
  1479.  
  1480. DRAW_LINE        ENDP
  1481.  
  1482.  
  1483.     ; ===== DAC COLOR REGISTER ROUTINES =====
  1484.  
  1485. ;=================================================
  1486. ;SET_DAC_REGISTER (Register%, Red%, Green%, Blue%)
  1487. ;=================================================
  1488. ;
  1489. ; Sets a single (RGB) Vga Palette Register
  1490. ;
  1491. ; ENTRY: Register = The DAC # to modify (0-255)
  1492. ;        Red      = The new Red Intensity (0-63)
  1493. ;        Green    = The new Green Intensity (0-63)
  1494. ;        Blue     = The new Blue Intensity (0-63)
  1495. ;
  1496. ; EXIT:  No meaningful values returned
  1497. ;
  1498.  
  1499. SDR_STACK   STRUC
  1500.                     DW  ?   ; BP
  1501.                     DD  ?   ; Caller
  1502.     SDR_Blue        DB  ?,? ; Blue Data Value
  1503.     SDR_Green       DB  ?,? ; Green Data Value
  1504.     SDR_Red         DB  ?,? ; Red Data Value
  1505.     SDR_Register    DB  ?,? ; Palette Register #
  1506. SDR_STACK   ENDS
  1507.  
  1508.     PUBLIC  SET_DAC_REGISTER
  1509.  
  1510. SET_DAC_REGISTER    PROC    FAR
  1511.  
  1512.     PUSH    BP                  ; Save BP
  1513.     MOV     BP, SP              ; Set up Stack Frame
  1514.  
  1515.     ; Select which DAC Register to modify
  1516.  
  1517.     OUT_8   DAC_WRITE_ADDR, [BP].SDR_Register
  1518.  
  1519.     MOV     DX, PEL_DATA_REG    ; Dac Data Register
  1520.     OUT_8   DX, [BP].SDR_Red    ; Set Red Intensity
  1521.     OUT_8   DX, [BP].SDR_Green  ; Set Green Intensity
  1522.     OUT_8   DX, [BP].SDR_Blue   ; Set Blue Intensity
  1523.  
  1524.     POP     BP                  ; Restore Registers
  1525.     RET     8                   ; Exit & Clean Up Stack
  1526.  
  1527. SET_DAC_REGISTER    ENDP
  1528.  
  1529. ;====================================================
  1530. ;GET_DAC_REGISTER (Register%, &Red%, &Green%, &Blue%)
  1531. ;====================================================
  1532. ;
  1533. ; Reads the RGB Values of a single Vga Palette Register
  1534. ;
  1535. ; ENTRY: Register = The DAC # to read (0-255)
  1536. ;        Red      = Offset to Red Variable in DS
  1537. ;        Green    = Offset to Green Variable in DS
  1538. ;        Blue     = Offset to Blue Variable in DS
  1539. ;
  1540. ; EXIT:  The values of the integer variables Red,
  1541. ;        Green, and Blue are set to the values
  1542. ;        taken from the specified DAC register.
  1543. ;
  1544.  
  1545. GDR_STACK   STRUC
  1546.                     DW  ?   ; BP
  1547.                     DD  ?   ; Caller
  1548.     GDR_Blue        DW  ?   ; Addr of Blue Data Value in DS
  1549.     GDR_Green       DW  ?   ; Addr of Green Data Value in DS
  1550.     GDR_Red         DW  ?   ; Addr of Red Data Value in DS
  1551.     GDR_Register    DB  ?,? ; Palette Register #
  1552. GDR_STACK   ENDS
  1553.  
  1554.     PUBLIC  GET_DAC_REGISTER
  1555.  
  1556. GET_DAC_REGISTER    PROC    FAR
  1557.  
  1558.     PUSH    BP                  ; Save BP
  1559.     MOV     BP, SP              ; Set up Stack Frame
  1560.  
  1561.     ; Select which DAC Register to read in
  1562.  
  1563.     OUT_8   DAC_READ_ADDR, [BP].GDR_Register
  1564.  
  1565.     MOV     DX, PEL_DATA_REG    ; Dac Data Register
  1566.     CLR     AX                  ; Clear AX
  1567.  
  1568.     IN      AL, DX              ; Read Red Value
  1569.     MOV     BX, [BP].GDR_Red    ; Get Address of Red%
  1570.     MOV     [BX], AX            ; *Red% = AX
  1571.  
  1572.     IN      AL, DX              ; Read Green Value
  1573.     MOV     BX, [BP].GDR_Green  ; Get Address of Green%
  1574.     MOV     [BX], AX            ; *Green% = AX
  1575.  
  1576.     IN      AL, DX              ; Read Blue Value
  1577.     MOV     BX, [BP].GDR_Blue   ; Get Address of Blue%
  1578.     MOV     [BX], AX            ; *Blue% = AX
  1579.  
  1580.     POP     BP                  ; Restore Registers
  1581.     RET     8                   ; Exit & Clean Up Stack
  1582.  
  1583. GET_DAC_REGISTER    ENDP
  1584.  
  1585.  
  1586. ;===========================================================
  1587. ;LOAD_DAC_REGISTERS (SEG PalData, StartReg%, EndReg%, Sync%)
  1588. ;===========================================================
  1589. ;
  1590. ; Sets a Block of Vga Palette Registers
  1591. ;
  1592. ; ENTRY: PalData  = Far Pointer to Block of palette data
  1593. ;        StartReg = First Register # in range to set (0-255)
  1594. ;        EndReg   = Last Register # in Range to set (0-255)
  1595. ;        Sync     = Wait for Vertical Retrace Flag (Boolean)
  1596. ;
  1597. ; EXIT:  No meaningful values returned
  1598. ;
  1599. ; NOTES: PalData is a linear array of 3 byte Palette values
  1600. ;        in the order: Red  (0-63), Green (0-63), Blue (0-63)
  1601. ;
  1602.  
  1603. LDR_STACK   STRUC
  1604.                     DW  ?x3 ; BP, DS, SI
  1605.                     DD  ?   ; Caller
  1606.     LDR_Sync        DW  ?   ; Vertical Sync Flag
  1607.     LDR_EndReg      DB  ?,? ; Last Register #
  1608.     LDR_StartReg    DB  ?,? ; First Register #
  1609.     LDR_PalData     DD  ?   ; Far Ptr to Palette Data
  1610. LDR_STACK   ENDS
  1611.  
  1612.     PUBLIC  LOAD_DAC_REGISTERS
  1613.  
  1614. LOAD_DAC_REGISTERS  PROC    FAR
  1615.  
  1616.     PUSHx   BP, DS, SI          ; Save Registers
  1617.     mov     BP, SP              ; Set up Stack Frame
  1618.  
  1619.     mov     AX, [BP].LDR_Sync   ; Get Vertical Sync Flag
  1620.     or      AX, AX              ; is Sync Flag = 0?
  1621.     jz      @LDR_Load           ; if so, skip call
  1622.  
  1623.     call    f SYNC_DISPLAY      ; wait for vsync
  1624.  
  1625.     ; Determine register #'s, size to copy, etc
  1626.  
  1627. @LDR_Load:
  1628.  
  1629.     lds     SI, [BP].LDR_PalData    ; DS:SI -> Palette Data
  1630.     mov     DX, DAC_WRITE_ADDR      ; DAC register # selector
  1631.  
  1632.     CLR     AX, BX                  ; Clear for byte loads
  1633.     mov     AL, [BP].LDR_StartReg   ; Get Start Register
  1634.     mov     BL, [BP].LDR_EndReg     ; Get End Register
  1635.  
  1636.     sub     BX, AX              ; BX = # of DAC registers -1
  1637.     inc     BX                  ; BX = # of DAC registers
  1638.     mov     CX, BX              ; CX = # of DAC registers
  1639.     add     CX, BX              ; CX =  "   " * 2
  1640.     add     CX, BX              ; CX =  "   " * 3
  1641.     cld                         ; Block OUTs forward
  1642.     out     DX, AL              ; set up correct register #
  1643.  
  1644.     ; Load a block of DAC Registers
  1645.  
  1646.     mov     DX, PEL_DATA_REG    ; Dac Data Register
  1647.  
  1648.     rep     outsb               ; block set DAC registers
  1649.  
  1650.     POPx    SI, DS, BP          ; Restore Registers
  1651.     ret     10                  ; Exit & Clean Up Stack
  1652.  
  1653. LOAD_DAC_REGISTERS  ENDP
  1654.  
  1655.  
  1656. ;====================================================
  1657. ;READ_DAC_REGISTERS (SEG PalData, StartReg%, EndReg%)
  1658. ;====================================================
  1659. ;
  1660. ; Reads a Block of Vga Palette Registers
  1661. ;
  1662. ; ENTRY: PalData  = Far Pointer to block to store palette data
  1663. ;        StartReg = First Register # in range to read (0-255)
  1664. ;        EndReg   = Last Register # in Range to read (0-255)
  1665. ;
  1666. ; EXIT:  No meaningful values returned
  1667. ;
  1668. ; NOTES: PalData is a linear array of 3 byte Palette values
  1669. ;        in the order: Red  (0-63), Green (0-63), Blue (0-63)
  1670. ;
  1671.  
  1672. RDR_STACK   STRUC
  1673.                     DW  ?x3 ; BP, ES, DI
  1674.                     DD  ?   ; Caller
  1675.     RDR_EndReg      DB  ?,? ; Last Register #
  1676.     RDR_StartReg    DB  ?,? ; First Register #
  1677.     RDR_PalData     DD  ?   ; Far Ptr to Palette Data
  1678. RDR_STACK   ENDS
  1679.  
  1680.     PUBLIC  READ_DAC_REGISTERS
  1681.  
  1682. READ_DAC_REGISTERS  PROC    FAR
  1683.  
  1684.     PUSHx   BP, ES, DI          ; Save Registers
  1685.     mov     BP, SP              ; Set up Stack Frame
  1686.  
  1687.     ; Determine register #'s, size to copy, etc
  1688.  
  1689.     les     DI, [BP].RDR_PalData    ; ES:DI -> Palette Buffer
  1690.     mov     DX, DAC_READ_ADDR       ; DAC register # selector
  1691.  
  1692.     CLR     AX, BX                  ; Clear for byte loads
  1693.     mov     AL, [BP].RDR_StartReg   ; Get Start Register
  1694.     mov     BL, [BP].RDR_EndReg     ; Get End Register
  1695.  
  1696.     sub     BX, AX              ; BX = # of DAC registers -1
  1697.     inc     BX                  ; BX = # of DAC registers
  1698.     mov     CX, BX              ; CX = # of DAC registers
  1699.     add     CX, BX              ; CX =  "   " * 2
  1700.     add     CX, BX              ; CX =  "   " * 3
  1701.     cld                         ; Block INs forward
  1702.  
  1703.     ; Read a block of DAC Registers
  1704.  
  1705.     out     DX, AL              ; set up correct register #
  1706.     mov     DX, PEL_DATA_REG    ; Dac Data Register
  1707.  
  1708.     rep     insb                ; block read DAC registers
  1709.  
  1710.     POPx    DI, ES, BP          ; Restore Registers
  1711.     ret     8                   ; Exit & Clean Up Stack
  1712.  
  1713. READ_DAC_REGISTERS  ENDP
  1714.  
  1715.  
  1716.     ; ===== PAGE FLIPPING AND SCROLLING ROUTINES =====
  1717.  
  1718. ;=========================
  1719. ;SET_ACTIVE_PAGE (PageNo%)
  1720. ;=========================
  1721. ;
  1722. ; Sets the active display Page to be used for future drawing
  1723. ;
  1724. ; ENTRY: PageNo = Display Page to make active
  1725. ;        (values: 0 to Number of Pages - 1)
  1726. ;
  1727. ; EXIT:  No meaningful values returned
  1728. ;
  1729.  
  1730. SAP_STACK   STRUC
  1731.                 DW  ?   ; BP
  1732.                 DD  ?   ; Caller
  1733.     SAP_Page    DW  ?   ; Page # for Drawing
  1734. SAP_STACK   ENDS
  1735.  
  1736.     PUBLIC  SET_ACTIVE_PAGE
  1737.  
  1738. SET_ACTIVE_PAGE PROC    FAR
  1739.  
  1740.     PUSH    BP                  ; Preserve Registers
  1741.     MOV     BP, SP              ; Set up Stack Frame
  1742.  
  1743.     MOV     BX, [BP].SAP_Page   ; Get Desired Page #
  1744.     CMP     BX, LAST_PAGE       ; Is Page # Valid?
  1745.     JAE     @SAP_Exit           ; IF Not, Do Nothing
  1746.  
  1747.     MOV     ACTIVE_PAGE, BX     ; Set Active Page #
  1748.  
  1749.     SHL     BX, 1               ; Scale Page # to Word
  1750.     MOV     AX, PAGE_ADDR[BX]   ; Get offset to Page
  1751.  
  1752.     MOV     CURRENT_PAGE, AX    ; And set for future LES's
  1753.  
  1754. @SAP_Exit:
  1755.     POP     BP                  ; Restore Registers
  1756.     RET     2                   ; Exit and Clean up Stack
  1757.  
  1758. SET_ACTIVE_PAGE ENDP
  1759.  
  1760.  
  1761. ;================
  1762. ;GET_ACTIVE_PAGE%
  1763. ;================
  1764. ;
  1765. ; Returns the Video Page # currently used for Drawing
  1766. ;
  1767. ; ENTRY: No Parameters are passed
  1768. ;
  1769. ; EXIT:  AX = Current Video Page used for Drawing
  1770. ;
  1771.  
  1772.     PUBLIC  GET_ACTIVE_PAGE
  1773.  
  1774. GET_ACTIVE_PAGE PROC    FAR
  1775.  
  1776.     MOV     AX, ACTIVE_PAGE     ; Get Active Page #
  1777.     RET                         ; Exit and Clean up Stack
  1778.  
  1779. GET_ACTIVE_PAGE ENDP
  1780.  
  1781.  
  1782. ;===============================
  1783. ;SET_DISPLAY_PAGE (DisplayPage%)
  1784. ;===============================
  1785. ;
  1786. ; Sets the currently visible display page.
  1787. ; When called this routine syncronizes the display
  1788. ; to the vertical blank.
  1789. ;
  1790. ; ENTRY: PageNo = Display Page to show on the screen
  1791. ;        (values: 0 to Number of Pages - 1)
  1792. ;
  1793. ; EXIT:  No meaningful values returned
  1794. ;
  1795.  
  1796. SDP_STACK   STRUC
  1797.                 DW  ?       ; BP
  1798.                 DD  ?       ; Caller
  1799.     SDP_Page    DW  ?       ; Page # to Display...
  1800. SDP_STACK   ENDS
  1801.  
  1802.     PUBLIC  SET_DISPLAY_PAGE
  1803.  
  1804. SET_DISPLAY_PAGE    PROC    FAR
  1805.  
  1806.     PUSH    BP                  ; Preserve Registers
  1807.     MOV     BP, SP              ; Set up Stack Frame
  1808.  
  1809.     MOV     BX, [BP].SDP_Page   ; Get Desired Page #
  1810.     CMP     BX, LAST_PAGE       ; Is Page # Valid?
  1811.     JAE     @SDP_Exit           ; IF Not, Do Nothing
  1812.  
  1813.     MOV     DISPLAY_PAGE, BX    ; Set Display Page #
  1814.  
  1815.     SHL     BX, 1               ; Scale Page # to Word
  1816.     MOV     CX, PAGE_ADDR[BX]   ; Get offset in memory to Page
  1817.     ADD     CX, CURRENT_MOFFSET ; Adjust for any scrolling
  1818.  
  1819.     ; Wait if we are currently in a Vertical Retrace
  1820.  
  1821.     MOV     DX, INPUT_1         ; Input Status #1 Register
  1822.  
  1823. @DP_WAIT0:
  1824.     IN      AL, DX              ; Get VGA status
  1825.     AND     AL, VERT_RETRACE    ; In Display mode yet?
  1826.     JNZ     @DP_WAIT0           ; If Not, wait for it
  1827.  
  1828.     ; Set the Start Display Address to the new page
  1829.  
  1830.     MOV     DX, CRTC_Index      ; We Change the VGA Sequencer
  1831.  
  1832.     MOV     AL, START_DISP_LO   ; Display Start Low Register
  1833.     MOV     AH, CL              ; Low 8 Bits of Start Addr
  1834.     OUT     DX, AX              ; Set Display Addr Low
  1835.  
  1836.     MOV     AL, START_DISP_HI   ; Display Start High Register
  1837.     MOV     AH, CH              ; High 8 Bits of Start Addr
  1838.     OUT     DX, AX              ; Set Display Addr High
  1839.  
  1840.     ; Wait for a Vertical Retrace to smooth out things
  1841.  
  1842.     MOV     DX, INPUT_1         ; Input Status #1 Register
  1843.  
  1844. @DP_WAIT1:
  1845.     IN      AL, DX              ; Get VGA status
  1846.     AND     AL, VERT_RETRACE    ; Vertical Retrace Start?
  1847.     JZ      @DP_WAIT1           ; If Not, wait for it
  1848.  
  1849.     ; Now Set Display Starting Address
  1850.  
  1851.  
  1852. @SDP_Exit:
  1853.     POP     BP                  ; Restore Registers
  1854.     RET     2                   ; Exit and Clean up Stack
  1855.  
  1856. SET_DISPLAY_PAGE    ENDP
  1857.  
  1858.  
  1859. ;=================
  1860. ;GET_DISPLAY_PAGE%
  1861. ;=================
  1862. ;
  1863. ; Returns the Video Page # currently displayed
  1864. ;
  1865. ; ENTRY: No Parameters are passed
  1866. ;
  1867. ; EXIT:  AX = Current Video Page being displayed
  1868. ;
  1869.  
  1870.     PUBLIC  GET_DISPLAY_PAGE
  1871.  
  1872. GET_DISPLAY_PAGE    PROC    FAR
  1873.  
  1874.     MOV     AX, DISPLAY_PAGE    ; Get Display Page #
  1875.     RET                         ; Exit & Clean Up Stack
  1876.  
  1877. GET_DISPLAY_PAGE    ENDP
  1878.  
  1879.  
  1880. ;=======================================
  1881. ;SET_WINDOW (DisplayPage%, Xpos%, Ypos%)
  1882. ;=======================================
  1883. ;
  1884. ; Since a Logical Screen can be larger than the Physical
  1885. ; Screen, Scrolling is possible.  This routine sets the
  1886. ; Upper Left Corner of the Screen to the specified Pixel.
  1887. ; Also Sets the Display page to simplify combined page
  1888. ; flipping and scrolling.  When called this routine
  1889. ; syncronizes the display to the vertical blank.
  1890. ;
  1891. ; ENTRY: DisplayPage = Display Page to show on the screen
  1892. ;        Xpos        = # of pixels to shift screen right
  1893. ;        Ypos        = # of lines to shift screen down
  1894. ;
  1895. ; EXIT:  No meaningful values returned
  1896. ;
  1897.  
  1898. SW_STACK    STRUC
  1899.                 DW  ?   ; BP
  1900.                 DD  ?   ; Caller
  1901.     SW_Ypos     DW  ?   ; Y pos of UL Screen Corner
  1902.     SW_Xpos     DW  ?   ; X pos of UL Screen Corner
  1903.     SW_Page     DW  ?   ; (new) Display Page
  1904. SW_STACK    ENDS
  1905.  
  1906.         PUBLIC SET_WINDOW
  1907.  
  1908. SET_WINDOW  PROC    FAR
  1909.  
  1910.     PUSH    BP                  ; Preserve Registers
  1911.     MOV     BP, SP              ; Set up Stack Frame
  1912.  
  1913.     ; Check if our Scroll Offsets are Valid
  1914.  
  1915.     MOV     BX, [BP].SW_Page    ; Get Desired Page #
  1916.     CMP     BX, LAST_PAGE       ; Is Page # Valid?
  1917.     JAE     @SW_Exit            ; IF Not, Do Nothing
  1918.  
  1919.     MOV     AX, [BP].SW_Ypos    ; Get Desired Y Offset
  1920.     CMP     AX, MAX_YOFFSET     ; Is it Within Limits?
  1921.     JA      @SW_Exit            ; if not, exit
  1922.  
  1923.     MOV     CX, [BP].SW_Xpos    ; Get Desired X Offset
  1924.     CMP     CX, MAX_XOFFSET     ; Is it Within Limits?
  1925.     JA      @SW_Exit            ; if not, exit
  1926.  
  1927.     ; Compute proper Display start address to use
  1928.  
  1929.     MUL     SCREEN_WIDTH        ; AX = YOffset * Line Width
  1930.     SHR     CX, 2               ; CX / 4 = Bytes into Line
  1931.     ADD     AX, CX              ; AX = Offset of Upper Left Pixel
  1932.  
  1933.     MOV     CURRENT_MOFFSET, AX ; Save Offset Info
  1934.  
  1935.     MOV     DISPLAY_PAGE, BX    ; Set Current Page #
  1936.     SHL     BX, 1               ; Scale Page # to Word
  1937.     ADD     AX, PAGE_ADDR[BX]   ; Get offset in VGA to Page
  1938.     MOV     BX, AX              ; BX = Desired Display Start
  1939.  
  1940.     MOV     DX, INPUT_1         ; Input Status #1 Register
  1941.  
  1942.     ; Wait if we are currently in a Vertical Retrace
  1943.  
  1944. @SW_WAIT0:
  1945.     IN      AL, DX              ; Get VGA status
  1946.     AND     AL, VERT_RETRACE    ; In Display mode yet?
  1947.     JNZ     @SW_WAIT0           ; If Not, wait for it
  1948.  
  1949.     ; Set the Start Display Address to the new window
  1950.  
  1951.     MOV     DX, CRTC_Index      ; We Change the VGA Sequencer
  1952.     MOV     AL, START_DISP_LO   ; Display Start Low Register
  1953.     MOV     AH, BL              ; Low 8 Bits of Start Addr
  1954.     OUT     DX, AX              ; Set Display Addr Low
  1955.  
  1956.     MOV     AL, START_DISP_HI   ; Display Start High Register
  1957.     MOV     AH, BH              ; High 8 Bits of Start Addr
  1958.     OUT     DX, AX              ; Set Display Addr High
  1959.  
  1960.     ; Wait for a Vertical Retrace to smooth out things
  1961.  
  1962.     MOV     DX, INPUT_1         ; Input Status #1 Register
  1963.  
  1964. @SW_WAIT1:
  1965.     IN      AL, DX              ; Get VGA status
  1966.     AND     AL, VERT_RETRACE    ; Vertical Retrace Start?
  1967.     JZ      @SW_WAIT1           ; If Not, wait for it
  1968.  
  1969.     ; Now Set the Horizontal Pixel Pan values
  1970.  
  1971.     OUT_8   ATTRIB_Ctrl, PIXEL_PAN_REG  ; Select Pixel Pan Register
  1972.  
  1973.     MOV     AX, [BP].SW_Xpos    ; Get Desired X Offset
  1974.     AND     AL, 03              ; Get # of Pixels to Pan (0-3)
  1975.     SHL     AL, 1               ; Shift for 256 Color Mode
  1976.     OUT     DX, AL              ; Fine tune the display!
  1977.  
  1978. @SW_Exit:
  1979.     POP     BP                  ; Restore Saved Registers
  1980.     RET     6                   ; Exit and Clean up Stack
  1981.  
  1982. SET_WINDOW        ENDP
  1983.  
  1984.  
  1985. ;=============
  1986. ;GET_X_OFFSET%
  1987. ;=============
  1988. ;
  1989. ; Returns the X coordinate of the Pixel currently display
  1990. ; in the upper left corner of the display
  1991. ;
  1992. ; ENTRY: No Parameters are passed
  1993. ;
  1994. ; EXIT:  AX = Current Horizontal Scroll Offset
  1995. ;
  1996.  
  1997.     PUBLIC  GET_X_OFFSET
  1998.  
  1999. GET_X_OFFSET    PROC    FAR
  2000.  
  2001.     MOV     AX, CURRENT_XOFFSET ; Get current horz offset
  2002.     RET                         ; Exit & Clean Up Stack
  2003.  
  2004. GET_X_OFFSET    ENDP
  2005.  
  2006.  
  2007. ;=============
  2008. ;GET_Y_OFFSET%
  2009. ;=============
  2010. ;
  2011. ; Returns the Y coordinate of the Pixel currently display
  2012. ; in the upper left corner of the display
  2013. ;
  2014. ; ENTRY: No Parameters are passed
  2015. ;
  2016. ; EXIT:  AX = Current Vertical Scroll Offset
  2017. ;
  2018.  
  2019.     PUBLIC  GET_Y_OFFSET
  2020.  
  2021. GET_Y_OFFSET    PROC    FAR
  2022.  
  2023.     MOV     AX, CURRENT_YOFFSET ; Get current vertical offset
  2024.     RET                         ; Exit & Clean Up Stack
  2025.  
  2026. GET_Y_OFFSET    ENDP
  2027.  
  2028.  
  2029. ;============
  2030. ;SYNC_DISPLAY
  2031. ;============
  2032. ;
  2033. ; Pauses the computer until the next Vertical Retrace starts
  2034. ;
  2035. ; ENTRY: No Parameters are passed
  2036. ;
  2037. ; EXIT:  No meaningful values returned
  2038. ;
  2039.  
  2040.     PUBLIC  SYNC_DISPLAY
  2041.  
  2042. SYNC_DISPLAY    PROC    FAR
  2043.  
  2044.     MOV     DX, INPUT_1         ; Input Status #1 Register
  2045.  
  2046.     ; Wait for any current retrace to end
  2047.  
  2048. @SD_WAIT0:
  2049.     IN      AL, DX              ; Get VGA status
  2050.     AND     AL, VERT_RETRACE    ; In Display mode yet?
  2051.     JNZ     @SD_WAIT0           ; If Not, wait for it
  2052.  
  2053.     ; Wait for the start of the next vertical retrace
  2054.  
  2055. @SD_WAIT1:
  2056.     IN      AL, DX              ; Get VGA status
  2057.     AND     AL, VERT_RETRACE    ; Vertical Retrace Start?
  2058.     JZ      @SD_WAIT1           ; If Not, wait for it
  2059.  
  2060.     RET                         ; Exit & Clean Up Stack
  2061.  
  2062. SYNC_DISPLAY    ENDP
  2063.  
  2064.  
  2065.     ; ===== TEXT DISPLAY ROUTINES =====
  2066.  
  2067. ;==================================================
  2068. ;GPRINTC (CharNum%, Xpos%, Ypos%, ColorF%, ColorB%)
  2069. ;==================================================
  2070. ;
  2071. ; Draws an ASCII Text Character using the currently selected
  2072. ; 8x8 font on the active display page.  It would be a simple
  2073. ; exercise to make this routine process variable height fonts.
  2074. ;
  2075. ; ENTRY: CharNum = ASCII character # to draw
  2076. ;        Xpos    = X position to draw Character at
  2077. ;        Ypos    = Y position of to draw Character at
  2078. ;        ColorF  = Color to draw text character in
  2079. ;        ColorB  = Color to set background to
  2080. ;
  2081. ; EXIT:  No meaningful values returned
  2082. ;
  2083.  
  2084. GPC_STACK   STRUC
  2085.     GPC_Width   DW  ?   ; Screen Width-1
  2086.     GPC_Lines   DB  ?,? ; Scan lines to Decode
  2087.     GPC_T_SETS  DW  ?   ; Saved Charset Segment
  2088.     GPC_T_SETO  DW  ?   ; Saved Charset Offset
  2089.                 DW  ?x4 ; DI, SI, DS, BP
  2090.                 DD  ?   ; Caller
  2091.     GPC_ColorB  DB  ?,? ; Background Color
  2092.     GPC_ColorF  DB  ?,? ; Text Color
  2093.     GPC_Ypos    DW  ?   ; Y Position to Print at
  2094.     GPC_Xpos    DW  ?   ; X position to Print at
  2095.     GPC_Char    DB  ?,? ; Character to Print
  2096. GPC_STACK   ENDS
  2097.  
  2098.         PUBLIC GPRINTC
  2099.  
  2100. GPRINTC     PROC    FAR
  2101.  
  2102.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  2103.     SUB     SP, 8               ; Allocate WorkSpace on Stack
  2104.     MOV     BP, SP              ; Set up Stack Frame
  2105.  
  2106.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  2107.  
  2108.     MOV     AX, SCREEN_WIDTH    ; Get Logical Line Width
  2109.     MOV     BX, AX              ; BX = Screen Width
  2110.     DEC     BX                  ;    = Screen Width-1
  2111.     MOV     [BP].GPC_Width, BX  ; Save for later use
  2112.  
  2113.     MUL     [BP].GPC_Ypos       ; Start of Line = Ypos * Width
  2114.     ADD     DI, AX              ; DI -> Start of Line Ypos
  2115.  
  2116.     MOV     AX, [BP].GPC_Xpos   ; Get Xpos of Character
  2117.     MOV     CX, AX              ; Save Copy of Xpos
  2118.     SHR     AX, 2               ; Bytes into Line = Xpos/4
  2119.     ADD     DI, AX              ; DI -> (Xpos, Ypos)
  2120.  
  2121.     ;Get Source ADDR of Character Bit Map  & Save
  2122.  
  2123.     MOV     AL, [BP].GPC_Char   ; Get Character #
  2124.     TEST    AL, 080h            ; Is Hi Bit Set?
  2125.     JZ      @GPC_LowChar        ; Nope, use low char set ptr
  2126.  
  2127.     AND     AL, 07Fh            ; Mask Out Hi Bit
  2128.     MOV     BX, CHARSET_HI      ; BX = Char Set Ptr:Offset
  2129.     MOV     DX, CHARSET_HI+2    ; DX = Char Set Ptr:Segment
  2130.     JMP     s @GPC_Set_Char     ; Go Setup Character Ptr
  2131.  
  2132. @GPC_LowChar:
  2133.  
  2134.     MOV     BX, CHARSET_LOW     ; BX = Char Set Ptr:Offset
  2135.     MOV     DX, CHARSET_LOW+2   ; DX = Char Set Ptr:Segment
  2136.  
  2137. @GPC_Set_Char:
  2138.     MOV     [BP].GPC_T_SETS, DX ; Save Segment on Stack
  2139.  
  2140.     MOV     AH, 0               ; Valid #'s are 0..127
  2141.     SHL     AX, 3               ; * 8 Bytes Per Bitmap
  2142.     ADD     BX, AX              ; BX = Offset of Selected char
  2143.     MOV     [BP].GPC_T_SETO, BX ; Save Offset on Stack
  2144.  
  2145.     AND     CX, PLANE_BITS      ; Get Plane #
  2146.     MOV     CH, ALL_PLANES      ; Get Initial Plane mask
  2147.     SHL     CH, CL              ; And shift into position
  2148.     AND     CH, ALL_PLANES      ; And mask to lower nibble
  2149.  
  2150.     MOV     AL, 04              ; 4-Plane # = # of initial
  2151.     SUB     AL, CL              ; shifts to align bit mask
  2152.     MOV     CL, AL              ; Shift Count for SHL
  2153.  
  2154.     ;Get segment of character map
  2155.  
  2156.     OUT_8   SC_Index, MAP_MASK  ; Setup Plane selections
  2157.     INC     DX                  ; DX -> SC_Data
  2158.  
  2159.     MOV     AL, 08              ; 8 Lines to Process
  2160.     MOV     [BP].GPC_Lines, AL  ; Save on Stack
  2161.  
  2162.     MOV     DS, [BP].GPC_T_SETS ; Point to character set
  2163.  
  2164. @GPC_DECODE_CHAR_BYTE:
  2165.  
  2166.     MOV     SI, [BP].GPC_T_SETO ; Get DS:SI = String
  2167.  
  2168.     MOV     BH, [SI]            ; Get Bit Map
  2169.     INC     SI                  ; Point to Next Line
  2170.     MOV     [BP].GPC_T_SETO, SI ; And save new Pointer...
  2171.  
  2172.     CLR     AX                  ; Clear AX
  2173.  
  2174.     CLR     BL                      ; Clear BL
  2175.     ROL     BX, CL                  ; BL holds left edge bits
  2176.     MOV     SI, BX                  ; Use as Table Index
  2177.     AND     SI, CHAR_BITS           ; Get Low Bits
  2178.     MOV     AL, Char_Plane_Data[SI] ; Get Mask in AL
  2179.     JZ      @GPC_NO_LEFT1BITS       ; Skip if No Pixels to set
  2180.  
  2181.     MOV     AH, [BP].GPC_ColorF ; Get Foreground Color
  2182.     OUT     DX, AL              ; Set up Screen Mask
  2183.     MOV     ES:[DI], AH         ; Write Foreground color
  2184.  
  2185. @GPC_NO_LEFT1BITS:
  2186.     XOR     AL, CH              ; Invert mask for Background
  2187.     JZ      @GPC_NO_LEFT0BITS   ; Hey, no need for this
  2188.  
  2189.     MOV     AH, [BP].GPC_ColorB ; Get background Color
  2190.     OUT     DX, AL              ; Set up Screen Mask
  2191.     MOV     ES:[DI], AH         ; Write Foreground color
  2192.  
  2193.     ;Now Do Middle/Last Band
  2194.  
  2195. @GPC_NO_LEFT0BITS:
  2196.     INC     DI                  ; Point to next Byte
  2197.     ROL     BX, 4               ; Shift 4 bits
  2198.  
  2199.     MOV     SI, BX                  ; Make Lookup Pointer
  2200.     AND     SI, CHAR_BITS           ; Get Low Bits
  2201.     MOV     AL, Char_Plane_Data[SI] ; Get Mask in AL
  2202.     JZ      @GPC_NO_MIDDLE1BITS     ; Skip if no pixels to set
  2203.  
  2204.     MOV     AH, [BP].GPC_ColorF ; Get Foreground Color
  2205.     OUT     DX, AL              ; Set up Screen Mask
  2206.     MOV     ES:[DI], AH         ; Write Foreground color
  2207.  
  2208. @GPC_NO_MIDDLE1BITS:
  2209.     XOR     AL, ALL_PLANES      ; Invert mask for Background
  2210.     JZ      @GPC_NO_MIDDLE0BITS ; Hey, no need for this
  2211.  
  2212.     MOV     AH, [BP].GPC_ColorB ; Get background Color
  2213.     OUT     DX, AL              ; Set up Screen Mask
  2214.     MOV     ES:[DI], AH         ; Write Foreground color
  2215.  
  2216. @GPC_NO_MIDDLE0BITS:
  2217.     XOR     CH, ALL_PLANES      ; Invert Clip Mask
  2218.     CMP     CL, 4               ; Aligned by 4?
  2219.     JZ      @GPC_NEXT_LINE      ; If so, Exit now..
  2220.  
  2221.     INC     DI                  ; Point to next Byte
  2222.     ROL     BX, 4               ; Shift 4 bits
  2223.  
  2224.     MOV     SI, BX                  ; Make Lookup Pointer
  2225.     AND     SI, CHAR_BITS           ; Get Low Bits
  2226.     MOV     AL, Char_Plane_Data[SI] ; Get Mask in AL
  2227.     JZ      @GPC_NO_RIGHT1BITS      ; Skip if No Pixels to set
  2228.  
  2229.     MOV     AH, [BP].GPC_ColorF ; Get Foreground Color
  2230.     OUT     DX, AL              ; Set up Screen Mask
  2231.     MOV     ES:[DI], AH         ; Write Foreground color
  2232.  
  2233. @GPC_NO_RIGHT1BITS:
  2234.  
  2235.     XOR     AL, CH              ; Invert mask for Background
  2236.     JZ      @GPC_NO_RIGHT0BITS  ; Hey, no need for this
  2237.  
  2238.     MOV     AH, [BP].GPC_ColorB ; Get background Color
  2239.     OUT     DX, AL              ; Set up Screen Mask
  2240.     MOV     ES:[DI], AH         ; Write Foreground color
  2241.  
  2242. @GPC_NO_RIGHT0BITS:
  2243.     DEC     DI                  ; Adjust for Next Line Advance
  2244.  
  2245. @GPC_NEXT_LINE:
  2246.     ADD     DI, [BP].GPC_Width  ; Point to Next Line
  2247.     XOR     CH, CHAR_BITS       ; Flip the Clip mask back
  2248.  
  2249.     DEC     [BP].GPC_Lines      ; Count Down Lines
  2250.     JZ      @GPC_EXIT           ; Ok... Done!
  2251.  
  2252.     JMP     @GPC_DECODE_CHAR_BYTE   ; Again! Hey!
  2253.  
  2254. @GPC_EXIT:
  2255.     ADD     SP, 08              ; Deallocate stack workspace
  2256.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  2257.     RET     10                  ; Exit and Clean up Stack
  2258.  
  2259. GPRINTC  ENDP
  2260.  
  2261.  
  2262. ;==========================================
  2263. ;TGPRINTC (CharNum%, Xpos%, Ypos%, ColorF%)
  2264. ;==========================================
  2265. ;
  2266. ; Transparently draws an ASCII Text Character using the
  2267. ; currently selected 8x8 font on the active display page.
  2268. ;
  2269. ; ENTRY: CharNum = ASCII character # to draw
  2270. ;        Xpos    = X position to draw Character at
  2271. ;        Ypos    = Y position of to draw Character at
  2272. ;        ColorF  = Color to draw text character in
  2273. ;
  2274. ; EXIT:  No meaningful values returned
  2275. ;
  2276.  
  2277. TGP_STACK   STRUC
  2278.     TGP_Width   DW  ?   ; Screen Width-1
  2279.     TGP_Lines   DB  ?,? ; Scan lines to Decode
  2280.     TGP_T_SETS  DW  ?   ; Saved Charset Segment
  2281.     TGP_T_SETO  DW  ?   ; Saved Charset Offset
  2282.                 DW  ?x4 ; DI, SI, DS, BP
  2283.                 DD  ?   ; Caller
  2284.     TGP_ColorF  DB  ?,? ; Text Color
  2285.     TGP_Ypos    DW  ?   ; Y Position to Print at
  2286.     TGP_Xpos    DW  ?   ; X position to Print at
  2287.     TGP_Char    DB  ?,? ; Character to Print
  2288. TGP_STACK   ENDS
  2289.  
  2290.         PUBLIC TGPRINTC
  2291.  
  2292. TGPRINTC    PROC    FAR
  2293.  
  2294.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  2295.     SUB     SP, 8               ; Allocate WorkSpace on Stack
  2296.     MOV     BP, SP              ; Set up Stack Frame
  2297.  
  2298.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  2299.  
  2300.     MOV     AX, SCREEN_WIDTH    ; Get Logical Line Width
  2301.     MOV     BX, AX              ; BX = Screen Width
  2302.     DEC     BX                  ;    = Screen Width-1
  2303.     MOV     [BP].TGP_Width, BX  ; Save for later use
  2304.  
  2305.     MUL     [BP].TGP_Ypos       ; Start of Line = Ypos * Width
  2306.     ADD     DI, AX              ; DI -> Start of Line Ypos
  2307.  
  2308.     MOV     AX, [BP].TGP_Xpos   ; Get Xpos of Character
  2309.     MOV     CX, AX              ; Save Copy of Xpos
  2310.     SHR     AX, 2               ; Bytes into Line = Xpos/4
  2311.     ADD     DI, AX              ; DI -> (Xpos, Ypos)
  2312.  
  2313.     ;Get Source ADDR of Character Bit Map  & Save
  2314.  
  2315.     MOV     AL, [BP].TGP_Char   ; Get Character #
  2316.     TEST    AL, 080h            ; Is Hi Bit Set?
  2317.     JZ      @TGP_LowChar        ; Nope, use low char set ptr
  2318.  
  2319.     AND     AL, 07Fh            ; Mask Out Hi Bit
  2320.     MOV     BX, CHARSET_HI      ; BX = Char Set Ptr:Offset
  2321.     MOV     DX, CHARSET_HI+2    ; DX = Char Set Ptr:Segment
  2322.     JMP     s @TGP_Set_Char     ; Go Setup Character Ptr
  2323.  
  2324. @TGP_LowChar:
  2325.  
  2326.     MOV     BX, CHARSET_LOW     ; BX = Char Set Ptr:Offset
  2327.     MOV     DX, CHARSET_LOW+2   ; DX = Char Set Ptr:Segment
  2328.  
  2329. @TGP_Set_Char:
  2330.     MOV     [BP].TGP_T_SETS, DX ; Save Segment on Stack
  2331.  
  2332.     MOV     AH, 0               ; Valid #'s are 0..127
  2333.     SHL     AX, 3               ; * 8 Bytes Per Bitmap
  2334.     ADD     BX, AX              ; BX = Offset of Selected char
  2335.     MOV     [BP].TGP_T_SETO, BX ; Save Offset on Stack
  2336.  
  2337.     AND     CX, PLANE_BITS      ; Get Plane #
  2338.     MOV     CH, ALL_PLANES      ; Get Initial Plane mask
  2339.     SHL     CH, CL              ; And shift into position
  2340.     AND     CH, ALL_PLANES      ; And mask to lower nibble
  2341.  
  2342.     MOV     AL, 04              ; 4-Plane # = # of initial
  2343.     SUB     AL, CL              ; shifts to align bit mask
  2344.     MOV     CL, AL              ; Shift Count for SHL
  2345.  
  2346.     ;Get segment of character map
  2347.  
  2348.     OUT_8   SC_Index, MAP_MASK  ; Setup Plane selections
  2349.     INC     DX                  ; DX -> SC_Data
  2350.  
  2351.     MOV     AL, 08              ; 8 Lines to Process
  2352.     MOV     [BP].TGP_Lines, AL  ; Save on Stack
  2353.  
  2354.     MOV     DS, [BP].TGP_T_SETS ; Point to character set
  2355.  
  2356. @TGP_DECODE_CHAR_BYTE:
  2357.  
  2358.     MOV     SI, [BP].TGP_T_SETO ; Get DS:SI = String
  2359.  
  2360.     MOV     BH, [SI]            ; Get Bit Map
  2361.     INC     SI                  ; Point to Next Line
  2362.     MOV     [BP].TGP_T_SETO, SI ; And save new Pointer...
  2363.  
  2364.     MOV     AH, [BP].TGP_ColorF ; Get Foreground Color
  2365.  
  2366.     CLR     BL                      ; Clear BL
  2367.     ROL     BX, CL                  ; BL holds left edge bits
  2368.     MOV     SI, BX                  ; Use as Table Index
  2369.     AND     SI, CHAR_BITS           ; Get Low Bits
  2370.     MOV     AL, Char_Plane_Data[SI] ; Get Mask in AL
  2371.     JZ      @TGP_NO_LEFT1BITS       ; Skip if No Pixels to set
  2372.  
  2373.     OUT     DX, AL              ; Set up Screen Mask
  2374.     MOV     ES:[DI], AH         ; Write Foreground color
  2375.  
  2376.     ;Now Do Middle/Last Band
  2377.  
  2378. @TGP_NO_LEFT1BITS:
  2379.  
  2380.     INC     DI                  ; Point to next Byte
  2381.     ROL     BX, 4               ; Shift 4 bits
  2382.  
  2383.     MOV     SI, BX                  ; Make Lookup Pointer
  2384.     AND     SI, CHAR_BITS           ; Get Low Bits
  2385.     MOV     AL, Char_Plane_Data[SI] ; Get Mask in AL
  2386.     JZ      @TGP_NO_MIDDLE1BITS     ; Skip if no pixels to set
  2387.  
  2388.     OUT     DX, AL              ; Set up Screen Mask
  2389.     MOV     ES:[DI], AH         ; Write Foreground color
  2390.  
  2391. @TGP_NO_MIDDLE1BITS:
  2392.     XOR     CH, ALL_PLANES      ; Invert Clip Mask
  2393.     CMP     CL, 4               ; Aligned by 4?
  2394.     JZ      @TGP_NEXT_LINE      ; If so, Exit now..
  2395.  
  2396.     INC     DI                  ; Point to next Byte
  2397.     ROL     BX, 4               ; Shift 4 bits
  2398.  
  2399.     MOV     SI, BX                  ; Make Lookup Pointer
  2400.     AND     SI, CHAR_BITS           ; Get Low Bits
  2401.     MOV     AL, Char_Plane_Data[SI] ; Get Mask in AL
  2402.     JZ      @TGP_NO_RIGHT1BITS      ; Skip if No Pixels to set
  2403.  
  2404.     OUT     DX, AL              ; Set up Screen Mask
  2405.     MOV     ES:[DI], AH         ; Write Foreground color
  2406.  
  2407. @TGP_NO_RIGHT1BITS:
  2408.  
  2409.     DEC     DI                  ; Adjust for Next Line Advance
  2410.  
  2411. @TGP_NEXT_LINE:
  2412.     ADD     DI, [BP].TGP_Width  ; Point to Next Line
  2413.     XOR     CH, CHAR_BITS       ; Flip the Clip mask back
  2414.  
  2415.     DEC     [BP].TGP_Lines      ; Count Down Lines
  2416.     JZ      @TGP_EXIT           ; Ok... Done!
  2417.  
  2418.     JMP     @TGP_DECODE_CHAR_BYTE   ; Again! Hey!
  2419.  
  2420. @TGP_EXIT:
  2421.     ADD     SP, 08              ; Deallocate stack workspace
  2422.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  2423.     RET     8                   ; Exit and Clean up Stack
  2424.  
  2425. TGPRINTC    ENDP
  2426.  
  2427.  
  2428. ;===============================================================
  2429. ;PRINT_STR (SEG String, MaxLen%, Xpos%, Ypos%, ColorF%, ColorB%)
  2430. ;===============================================================
  2431. ;
  2432. ; Routine to quickly Print a null terminated ASCII string on the
  2433. ; active display page up to a maximum length.
  2434. ;
  2435. ; ENTRY: String  = Far Pointer to ASCII string to print
  2436. ;        MaxLen  = # of characters to print if no null found
  2437. ;        Xpos    = X position to draw Text at
  2438. ;        Ypos    = Y position of to draw Text at
  2439. ;        ColorF  = Color to draw text in
  2440. ;        ColorB  = Color to set background to
  2441. ;
  2442. ; EXIT:  No meaningful values returned
  2443. ;
  2444.  
  2445. PS_STACK    STRUC
  2446.                 DW  ?x4 ; DI, SI, DS, BP
  2447.                 DD  ?   ; Caller
  2448.     PS_ColorB   DW  ?   ; Background Color
  2449.     PS_ColorF   DW  ?   ; Text Color
  2450.     PS_Ypos     DW  ?   ; Y Position to Print at
  2451.     PS_Xpos     DW  ?   ; X position to Print at
  2452.     PS_Len      DW  ?   ; Maximum Length of string to print
  2453.     PS_Text     DW  ?,? ; Far Ptr to Text String
  2454. PS_STACK    ENDS
  2455.  
  2456.         PUBLIC  PRINT_STR
  2457.  
  2458. PRINT_STR   PROC    FAR
  2459.  
  2460.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  2461.     MOV     BP, SP              ; Set up Stack Frame
  2462.  
  2463. @PS_Print_It:
  2464.  
  2465.     MOV     CX, [BP].PS_Len     ; Get Remaining text Length
  2466.     JCXZ    @PS_Exit            ; Exit when out of text
  2467.  
  2468.     LES     DI, d [BP].PS_Text  ; ES:DI -> Current Char in Text
  2469.     MOV     AL, ES:[DI]         ; AL = Text Character
  2470.     AND     AX, 00FFh           ; Clear High Word
  2471.     JZ      @PS_Exit            ; Exit if null character
  2472.  
  2473.     DEC     [BP].PS_Len         ; Remaining Text length--
  2474.     INC     [BP].PS_Text        ; Point to Next text char
  2475.  
  2476.     ; Set up Call to GPRINTC
  2477.  
  2478.     PUSH    AX                  ; Set Character Parameter
  2479.     MOV     BX, [BP].PS_Xpos    ; Get Xpos
  2480.     PUSH    BX                  ; Set Xpos Parameter
  2481.     ADD     BX, 8               ; Advance 1 Char to Right
  2482.     MOV     [BP].PS_Xpos, BX    ; Save for next time through
  2483.  
  2484.     MOV     BX, [BP].PS_Ypos    ; Get Ypos
  2485.     PUSH    BX                  ; Set Ypos Parameter
  2486.  
  2487.     MOV     BX, [BP].PS_ColorF  ; Get Text Color
  2488.     PUSH    BX                  ; Set ColorF Parameter
  2489.  
  2490.     MOV     BX, [BP].PS_ColorB  ; Get Background Color
  2491.     PUSH    BX                  ; Set ColorB Parameter
  2492.  
  2493.     CALL    f GPRINTC           ; Print Character!
  2494.     JMP     s @PS_Print_It      ; Process next character
  2495.  
  2496. @PS_Exit:
  2497.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  2498.     RET     14                  ; Exit and Clean up Stack
  2499.  
  2500. PRINT_STR  ENDP
  2501.  
  2502.  
  2503. ;================================================================
  2504. ;TPRINT_STR (SEG String, MaxLen%, Xpos%, Ypos%, ColorF%, ColorB%)
  2505. ;================================================================
  2506. ;
  2507. ; Routine to quickly transparently Print a null terminated ASCII
  2508. ; string on the active display page up to a maximum length.
  2509. ;
  2510. ; ENTRY: String  = Far Pointer to ASCII string to print
  2511. ;        MaxLen  = # of characters to print if no null found
  2512. ;        Xpos    = X position to draw Text at
  2513. ;        Ypos    = Y position of to draw Text at
  2514. ;        ColorF  = Color to draw text in
  2515. ;
  2516. ; EXIT:  No meaningful values returned
  2517. ;
  2518.  
  2519. TPS_STACK   STRUC
  2520.                 DW  ?x4 ; DI, SI, DS, BP
  2521.                 DD  ?   ; Caller
  2522.     TPS_ColorF  DW  ?   ; Text Color
  2523.     TPS_Ypos    DW  ?   ; Y Position to Print at
  2524.     TPS_Xpos    DW  ?   ; X position to Print at
  2525.     TPS_Len     DW  ?   ; Maximum Length of string to print
  2526.     TPS_Text    DW  ?,? ; Far Ptr to Text String
  2527. TPS_STACK   ENDS
  2528.  
  2529.         PUBLIC  TPRINT_STR
  2530.  
  2531. TPRINT_STR  PROC    FAR
  2532.  
  2533.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  2534.     MOV     BP, SP              ; Set up Stack Frame
  2535.  
  2536. @TPS_Print_It:
  2537.  
  2538.     MOV     CX, [BP].TPS_Len    ; Get Remaining text Length
  2539.     JCXZ    @TPS_Exit           ; Exit when out of text
  2540.  
  2541.     LES     DI, d [BP].TPS_Text ; ES:DI -> Current Char in Text
  2542.     MOV     AL, ES:[DI]         ; AL = Text Character
  2543.     AND     AX, 00FFh           ; Clear High Word
  2544.     JZ      @TPS_Exit           ; Exit if null character
  2545.  
  2546.     DEC     [BP].TPS_Len        ; Remaining Text length--
  2547.     INC     [BP].TPS_Text       ; Point to Next text char
  2548.  
  2549.     ; Set up Call to TGPRINTC
  2550.  
  2551.     PUSH    AX                  ; Set Character Parameter
  2552.     MOV     BX, [BP].TPS_Xpos   ; Get Xpos
  2553.     PUSH    BX                  ; Set Xpos Parameter
  2554.     ADD     BX, 8               ; Advance 1 Char to Right
  2555.     MOV     [BP].TPS_Xpos, BX   ; Save for next time through
  2556.  
  2557.     MOV     BX, [BP].TPS_Ypos   ; Get Ypos
  2558.     PUSH    BX                  ; Set Ypos Parameter
  2559.  
  2560.     MOV     BX, [BP].TPS_ColorF ; Get Text Color
  2561.     PUSH    BX                  ; Set ColorF Parameter
  2562.  
  2563.     CALL    f TGPRINTC          ; Print Character!
  2564.     JMP     s @TPS_Print_It     ; Process next character
  2565.  
  2566. @TPS_Exit:
  2567.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  2568.     RET     12                  ; Exit and Clean up Stack
  2569.  
  2570. TPRINT_STR  ENDP
  2571.  
  2572.  
  2573. ;===========================================
  2574. ;SET_DISPLAY_FONT(SEG FontData, FontNumber%)
  2575. ;===========================================
  2576. ;
  2577. ; Allows the user to specify their own font data for
  2578. ; wither the lower or upper 128 characters.
  2579. ;
  2580. ; ENTRY: FontData   = Far Pointer to Font Bitmaps
  2581. ;        FontNumber = Which half of set this is
  2582. ;                   = 0, Lower 128 characters
  2583. ;                   = 1, Upper 128 characters
  2584. ;
  2585. ; EXIT:  No meaningful values returned
  2586. ;
  2587.  
  2588. SDF_STACK   STRUC
  2589.                 DW  ?   ; BP
  2590.                 DD  ?   ; Caller
  2591.     SDF_Which   DW  ?   ; Hi Table/Low Table Flag
  2592.     SDF_Font    DD  ?   ; Far Ptr to Font Table
  2593. SDF_STACK   ENDS
  2594.  
  2595.     PUBLIC  SET_DISPLAY_FONT
  2596.  
  2597. SET_DISPLAY_FONT    PROC    FAR
  2598.  
  2599.     PUSH    BP                  ; Preserve Registers
  2600.     MOV     BP, SP              ; Set up Stack Frame
  2601.  
  2602.     LES     DI, [BP].SDF_Font   ; Get Far Ptr to Font
  2603.  
  2604.     MOV     SI, o CHARSET_LOW   ; Assume Lower 128 chars
  2605.     TEST    [BP].SDF_Which, 1   ; Font #1 selected?
  2606.     JZ      @SDF_Set_Font       ; If not, skip ahead
  2607.  
  2608.     MOV     SI, o CHARSET_HI    ; Ah, really it's 128-255
  2609.  
  2610. @SDF_Set_Font:
  2611.     MOV     [SI], DI            ; Set Font Pointer Offset
  2612.     MOV     [SI+2], ES          ; Set Font Pointer Segment
  2613.  
  2614.     POP     BP                  ; Restore Registers
  2615.     RET     6                   ; We are Done.. Outa here
  2616.  
  2617. SET_DISPLAY_FONT    ENDP
  2618.  
  2619.  
  2620.     ; ===== BITMAP (SPRITE) DISPLAY ROUTINES =====
  2621.  
  2622. ;======================================================
  2623. ;DRAW_BITMAP (SEG Image, Xpos%, Ypos%, Width%, Height%)
  2624. ;======================================================
  2625. ;
  2626. ; Draws a variable sized Graphics Bitmap such as a
  2627. ; picture or an Icon on the current Display Page in
  2628. ; Mode X.  The Bitmap is stored in a linear byte array
  2629. ; corresponding to (0,0) (1,0), (2,0) .. (Width, Height)
  2630. ; This is the same linear manner as mode 13h graphics.
  2631. ;
  2632. ; ENTRY: Image  = Far Pointer to Bitmap Data
  2633. ;        Xpos   = X position to Place Upper Left pixel at
  2634. ;        Ypos   = Y position to Place Upper Left pixel at
  2635. ;        Width  = Width of the Bitmap in Pixels
  2636. ;        Height = Height of the Bitmap in Pixels
  2637. ;
  2638. ; EXIT:  No meaningful values returned
  2639. ;
  2640.  
  2641. DB_STACK    STRUC
  2642.     DB_LineO    DW  ?   ; Offset to Next Line
  2643.     DB_PixCount DW  ?   ; (Minimum) # of Pixels/Line
  2644.     DB_Start    DW  ?   ; Addr of Upper Left Pixel
  2645.     DB_PixSkew  DW  ?   ; # of bytes to Adjust EOL
  2646.     DB_SkewFlag DW  ?   ; Extra Pix on Plane Flag
  2647.                 DW  ?x4 ; DI, SI, DS, BP
  2648.                 DD  ?   ; Caller
  2649.     DB_Height   DW  ?   ; Height of Bitmap in Pixels
  2650.     DB_Width    DW  ?   ; Width of Bitmap in Pixels
  2651.     DB_Ypos     DW  ?   ; Y position to Draw Bitmap at
  2652.     DB_Xpos     DW  ?   ; X position to Draw Bitmap at
  2653.     DB_Image    DD  ?   ; Far Pointer to Graphics Bitmap
  2654. DB_STACK    ENDS
  2655.  
  2656.         PUBLIC    DRAW_BITMAP
  2657.  
  2658. DRAW_BITMAP PROC    FAR
  2659.  
  2660.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  2661.     SUB     SP, 10              ; Allocate workspace
  2662.     MOV     BP, SP              ; Set up Stack Frame
  2663.  
  2664.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  2665.     CLD                         ; Direction Flag = Forward
  2666.  
  2667.     MOV     AX, [BP].DB_Ypos    ; Get UL Corner Ypos
  2668.     MUL     SCREEN_WIDTH        ; AX = Offset to Line Ypos
  2669.  
  2670.     MOV     BX, [BP].DB_Xpos    ; Get UL Corner Xpos
  2671.     MOV     CL, BL              ; Save Plane # in CL
  2672.     SHR     BX, 2               ; Xpos/4 = Offset Into Line
  2673.  
  2674.     ADD     DI, AX              ; ES:DI -> Start of Line
  2675.     ADD     DI, BX              ; ES:DI -> Upper Left Pixel
  2676.     MOV     [BP].DB_Start, DI   ; Save Starting Addr
  2677.  
  2678.     ; Compute line to line offset
  2679.  
  2680.     MOV     BX, [BP].DB_Width   ; Get Width of Image
  2681.     MOV     DX, BX              ; Save Copy in DX
  2682.     SHR     BX, 2               ; /4 = width in bands
  2683.     MOV     AX, SCREEN_WIDTH    ; Get Screen Width
  2684.     SUB     AX, BX              ; - (Bitmap Width/4)
  2685.  
  2686.     MOV     [BP].DB_LineO, AX       ; Save Line Width offset
  2687.     MOV     [BP].DB_PixCount, BX    ; Minimum # pix to copy
  2688.  
  2689.     AND     DX, PLANE_BITS          ; Get "partial band" size (0-3)
  2690.     MOV     [BP].DB_PixSkew, DX     ; Also End of Line Skew
  2691.     MOV     [BP].DB_SkewFlag, DX    ; Save as Flag/Count
  2692.  
  2693.     AND     CX, PLANE_BITS      ; CL = Starting Plane #
  2694.     MOV     AX, MAP_MASK_PLANE2 ; Plane Mask & Plane Select
  2695.     SHL     AH, CL              ; Select correct Plane
  2696.     OUT_16  SC_Index, AX        ; Select Plane...
  2697.     MOV     BH, AH              ; BH = Saved Plane Mask
  2698.     MOV     BL, 4               ; BL = Planes to Copy
  2699.  
  2700. @DB_COPY_PLANE:
  2701.  
  2702.     LDS     SI, [BP].DB_Image   ; DS:SI-> Source Image
  2703.     MOV     DX, [BP].DB_Height  ; # of Lines to Copy
  2704.     MOV     DI, [BP].DB_Start   ; ES:DI-> Dest pos
  2705.  
  2706. @DB_COPY_LINE:
  2707.     MOV     CX, [BP].DB_PixCount    ; Min # to copy
  2708.  
  2709.     TEST    CL, 0FCh            ; 16+PixWide?
  2710.     JZ      @DB_COPY_REMAINDER  ; Nope...
  2711.  
  2712.     ; Pixel Copy loop has been unrolled to x4
  2713.  
  2714. @DB_COPY_LOOP:
  2715.     MOVSB                       ; Copy Bitmap Pixel
  2716.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2717.     MOVSB                       ; Copy Bitmap Pixel
  2718.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2719.     MOVSB                       ; Copy Bitmap Pixel
  2720.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2721.     MOVSB                       ; Copy Bitmap Pixel
  2722.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2723.  
  2724.     SUB     CL, 4               ; Pixels to Copy=-4
  2725.     TEST    CL, 0FCh            ; 4+ Pixels Left?
  2726.     JNZ     @DB_COPY_LOOP       ; if so, do another block
  2727.  
  2728. @DB_COPY_REMAINDER:
  2729.     JCXZ    @DB_NEXT_LINE       ; Any Pixels left on line
  2730.  
  2731. @DB_COPY2:
  2732.     MOVSB                       ; Copy Bitmap Pixel
  2733.     ADD     SI,3                ; Skip to Next Byte in same plane
  2734.     LOOPx   CX, @DB_COPY2       ; Pixels to Copy--, Loop until done
  2735.  
  2736. @DB_NEXT_LINE:
  2737.  
  2738.     ; any Partial Pixels? (some planes only)
  2739.  
  2740.     OR      CX, [BP].DB_SkewFlag    ; Get Skew Count
  2741.     JZ      @DB_NEXT2               ; if no partial pixels
  2742.  
  2743.     MOVSB                       ; Copy Bitmap Pixel
  2744.     DEC     DI                  ; Back up to align
  2745.     DEC     SI                  ; Back up to align
  2746.  
  2747. @DB_NEXT2:
  2748.     ADD     SI, [BP].DB_PixSkew ; Adjust Skew
  2749.     ADD     DI, [BP].DB_LineO   ; Set to Next Display Line
  2750.     LOOPx   DX, @DB_COPY_LINE   ; Lines to Copy--, Loop if more
  2751.  
  2752.     ; Copy Next Plane....
  2753.  
  2754.     DEC     BL                  ; Planes to Go--
  2755.     JZ      @DB_Exit            ; Hey! We are done
  2756.  
  2757.     ROL     BH, 1               ; Next Plane in line...
  2758.     OUT_8   SC_Data, BH         ; Select Plane
  2759.  
  2760.     CMP     AL, 12h             ; Carry Set if AL=11h
  2761.     ADC     [BP].DB_Start, 0    ; Screen Addr =+Carry
  2762.     INC     w [BP].DB_Image     ; Start @ Next Byte
  2763.  
  2764.     SUB     [BP].DB_SkewFlag, 1 ; Reduce Planes to Skew
  2765.     ADC     [BP].DB_SkewFlag, 0 ; Back to 0 if it was -1
  2766.  
  2767.     JMP     s @DB_COPY_PLANE    ; Go Copy the Next Plane
  2768.  
  2769. @DB_Exit:
  2770.     ADD     SP, 10              ; Deallocate workspace
  2771.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  2772.     RET     12                  ; Exit and Clean up Stack
  2773.  
  2774. DRAW_BITMAP   ENDP
  2775.  
  2776.  
  2777. ;=======================================================
  2778. ;TDRAW_BITMAP (SEG Image, Xpos%, Ypos%, Width%, Height%)
  2779. ;=======================================================
  2780. ;
  2781. ; Transparently Draws a variable sized Graphics Bitmap
  2782. ; such as a picture or an Icon on the current Display Page
  2783. ; in Mode X.  Pixels with a value of 0 are not drawn,
  2784. ; leaving the previous "background" contents intact.
  2785. ;
  2786. ; The Bitmap format is the same as for the DRAW_BITMAP function.
  2787. ;
  2788. ; ENTRY: Image  = Far Pointer to Bitmap Data
  2789. ;        Xpos   = X position to Place Upper Left pixel at
  2790. ;        Ypos   = Y position to Place Upper Left pixel at
  2791. ;        Width  = Width of the Bitmap in Pixels
  2792. ;        Height = Height of the Bitmap in Pixels
  2793. ;
  2794. ; EXIT:  No meaningful values returned
  2795. ;
  2796.  
  2797. TB_STACK    STRUC
  2798.     TB_LineO    DW  ?   ; Offset to Next Line
  2799.     TB_PixCount DW  ?   ; (Minimum) # of Pixels/Line
  2800.     TB_Start    DW  ?   ; Addr of Upper Left Pixel
  2801.     TB_PixSkew  DW  ?   ; # of bytes to Adjust EOL
  2802.     TB_SkewFlag DW  ?   ; Extra Pix on Plane Flag
  2803.                 DW  ?x4 ; DI, SI, DS, BP
  2804.                 DD  ?   ; Caller
  2805.     TB_Height   DW  ?   ; Height of Bitmap in Pixels
  2806.     TB_Width    DW  ?   ; Width of Bitmap in Pixels
  2807.     TB_Ypos     DW  ?   ; Y position to Draw Bitmap at
  2808.     TB_Xpos     DW  ?   ; X position to Draw Bitmap at
  2809.     TB_Image    DD  ?   ; Far Pointer to Graphics Bitmap
  2810. TB_STACK    ENDS
  2811.  
  2812.         PUBLIC    TDRAW_BITMAP
  2813.  
  2814. TDRAW_BITMAP    PROC    FAR
  2815.  
  2816.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  2817.     SUB     SP, 10              ; Allocate workspace
  2818.     MOV     BP, SP              ; Set up Stack Frame
  2819.  
  2820.     LES     DI, d CURRENT_PAGE  ; Point to Active VGA Page
  2821.     CLD                         ; Direction Flag = Forward
  2822.  
  2823.     MOV     AX, [BP].TB_Ypos    ; Get UL Corner Ypos
  2824.     MUL     SCREEN_WIDTH        ; AX = Offset to Line Ypos
  2825.  
  2826.     MOV     BX, [BP].TB_Xpos    ; Get UL Corner Xpos
  2827.     MOV     CL, BL              ; Save Plane # in CL
  2828.     SHR     BX, 2               ; Xpos/4 = Offset Into Line
  2829.  
  2830.     ADD     DI, AX              ; ES:DI -> Start of Line
  2831.     ADD     DI, BX              ; ES:DI -> Upper Left Pixel
  2832.     MOV     [BP].TB_Start, DI   ; Save Starting Addr
  2833.  
  2834.     ; Compute line to line offset
  2835.  
  2836.     MOV     BX, [BP].TB_Width   ; Get Width of Image
  2837.     MOV     DX, BX              ; Save Copy in DX
  2838.     SHR     BX, 2               ; /4 = width in bands
  2839.     MOV     AX, SCREEN_WIDTH    ; Get Screen Width
  2840.     SUB     AX, BX              ; - (Bitmap Width/4)
  2841.  
  2842.     MOV     [BP].TB_LineO, AX       ; Save Line Width offset
  2843.     MOV     [BP].TB_PixCount, BX    ; Minimum # pix to copy
  2844.  
  2845.     AND     DX, PLANE_BITS          ; Get "partial band" size (0-3)
  2846.     MOV     [BP].TB_PixSkew, DX     ; Also End of Line Skew
  2847.     MOV     [BP].TB_SkewFlag, DX    ; Save as Flag/Count
  2848.  
  2849.     AND     CX, PLANE_BITS      ; CL = Starting Plane #
  2850.     MOV     AX, MAP_MASK_PLANE2 ; Plane Mask & Plane Select
  2851.     SHL     AH, CL              ; Select correct Plane
  2852.     OUT_16  SC_Index, AX        ; Select Plane...
  2853.     MOV     BH, AH              ; BH = Saved Plane Mask
  2854.     MOV     BL, 4               ; BL = Planes to Copy
  2855.  
  2856. @TB_COPY_PLANE:
  2857.  
  2858.     LDS     SI, [BP].TB_Image   ; DS:SI-> Source Image
  2859.     MOV     DX, [BP].TB_Height  ; # of Lines to Copy
  2860.     MOV     DI, [BP].TB_Start   ; ES:DI-> Dest pos
  2861.  
  2862.     ; Here AH is set with the value to be considered
  2863.     ; "Transparent".  It can be changed!
  2864.  
  2865.     MOV     AH, 0               ; Value to Detect 0
  2866.  
  2867. @TB_COPY_LINE:
  2868.     MOV     CX, [BP].TB_PixCount    ; Min # to copy
  2869.  
  2870.     TEST    CL, 0FCh            ; 16+PixWide?
  2871.     JZ      @TB_COPY_REMAINDER  ; Nope...
  2872.  
  2873.     ; Pixel Copy loop has been unrolled to x4
  2874.  
  2875. @TB_COPY_LOOP:
  2876.     LODSB                       ; Get Pixel Value in AL
  2877.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2878.     CMP     AL, AH              ; It is "Transparent"?
  2879.     JE      @TB_SKIP_01         ; Skip ahead if so
  2880.     MOV     ES:[DI], AL         ; Copy Pixel to VGA screen
  2881.  
  2882. @TB_SKIP_01:
  2883.     LODSB                       ; Get Pixel Value in AL
  2884.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2885.     CMP     AL, AH              ; It is "Transparent"?
  2886.     JE      @TB_SKIP_02         ; Skip ahead if so
  2887.     MOV     ES:[DI+1], AL       ; Copy Pixel to VGA screen
  2888.  
  2889. @TB_SKIP_02:
  2890.     LODSB                       ; Get Pixel Value in AL
  2891.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2892.     CMP     AL, AH              ; It is "Transparent"?
  2893.     JE      @TB_SKIP_03         ; Skip ahead if so
  2894.     MOV     ES:[DI+2], AL       ; Copy Pixel to VGA screen
  2895.  
  2896. @TB_SKIP_03:
  2897.     LODSB                       ; Get Pixel Value in AL
  2898.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2899.     CMP     AL, AH              ; It is "Transparent"?
  2900.     JE      @TB_SKIP_04         ; Skip ahead if so
  2901.     MOV     ES:[DI+3], AL       ; Copy Pixel to VGA screen
  2902.  
  2903. @TB_SKIP_04:
  2904.     ADD     DI, 4               ; Adjust Pixel Write Location
  2905.     SUB     CL, 4               ; Pixels to Copy=-4
  2906.     TEST    CL, 0FCh            ; 4+ Pixels Left?
  2907.     JNZ     @TB_COPY_LOOP       ; if so, do another block
  2908.  
  2909. @TB_COPY_REMAINDER:
  2910.     JCXZ    @TB_NEXT_LINE       ; Any Pixels left on line
  2911.  
  2912. @TB_COPY2:
  2913.     LODSB                       ; Get Pixel Value in AL
  2914.     ADD     SI, 3               ; Skip to Next Byte in same plane
  2915.     CMP     AL, AH              ; It is "Transparent"?
  2916.     JE      @TB_SKIP_05         ; Skip ahead if so
  2917.     MOV     ES:[DI], AL         ; Copy Pixel to VGA screen
  2918.  
  2919. @TB_SKIP_05:
  2920.     INC     DI                  ; Advance Dest Addr
  2921.     LOOPx   CX, @TB_COPY2       ; Pixels to Copy--, Loop until done
  2922.  
  2923. @TB_NEXT_LINE:
  2924.  
  2925.     ; any Partial Pixels? (some planes only)
  2926.  
  2927.     OR      CX, [BP].TB_SkewFlag    ; Get Skew Count
  2928.     JZ      @TB_NEXT2               ; if no partial pixels
  2929.  
  2930.     LODSB                       ; Get Pixel Value in AL
  2931.     DEC     SI                  ; Backup to Align
  2932.     CMP     AL, AH              ; It is "Transparent"?
  2933.     JE      @TB_NEXT2           ; Skip ahead if so
  2934.     MOV     ES:[DI], AL         ; Copy Pixel to VGA screen
  2935.  
  2936. @TB_NEXT2:
  2937.     ADD     SI, [BP].TB_PixSkew ; Adjust Skew
  2938.     ADD     DI, [BP].TB_LineO   ; Set to Next Display Line
  2939.     LOOPx   DX, @TB_COPY_LINE   ; Lines to Copy--, Loop if More
  2940.  
  2941.     ;Copy Next Plane....
  2942.  
  2943.     DEC     BL                  ; Planes to Go--
  2944.     JZ      @TB_Exit            ; Hey! We are done
  2945.  
  2946.     ROL     BH, 1               ; Next Plane in line...
  2947.     OUT_8   SC_Data, BH         ; Select Plane
  2948.  
  2949.     CMP     AL, 12h             ; Carry Set if AL=11h
  2950.     ADC     [BP].TB_Start, 0    ; Screen Addr =+Carry
  2951.     INC     w [BP].TB_Image     ; Start @ Next Byte
  2952.  
  2953.     SUB     [BP].TB_SkewFlag, 1 ; Reduce Planes to Skew
  2954.     ADC     [BP].TB_SkewFlag, 0 ; Back to 0 if it was -1
  2955.  
  2956.     JMP     @TB_COPY_PLANE      ; Go Copy the next Plane
  2957.  
  2958. @TB_Exit:
  2959.     ADD     SP, 10              ; Deallocate workspace
  2960.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  2961.     RET     12                  ; Exit and Clean up Stack
  2962.  
  2963. TDRAW_BITMAP    ENDP
  2964.  
  2965.  
  2966.     ; ==== VIDEO MEMORY to VIDEO MEMORY COPY ROUTINES =====
  2967.  
  2968. ;==================================
  2969. ;COPY_PAGE (SourcePage%, DestPage%)
  2970. ;==================================
  2971. ;
  2972. ; Duplicate on display page onto another
  2973. ;
  2974. ; ENTRY: SourcePage = Display Page # to Duplicate
  2975. ;        DestPage   = Display Page # to hold copy
  2976. ;
  2977. ; EXIT:  No meaningful values returned
  2978. ;
  2979.  
  2980. CP_STACK    STRUC
  2981.                 DW  ?x4 ; DI, SI, DS, BP
  2982.                 DD  ?   ; Caller
  2983.     CP_DestP    DW  ?   ; Page to hold copied image
  2984.     CP_SourceP  DW  ?   ; Page to Make copy from
  2985. CP_STACK    ENDS
  2986.  
  2987.         PUBLIC    COPY_PAGE
  2988.  
  2989. COPY_PAGE   PROC    FAR
  2990.  
  2991.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  2992.     MOV     BP, SP              ; Set up Stack Frame
  2993.     CLD                         ; Block Xfer Forwards
  2994.  
  2995.     ; Make sure Page #'s are valid
  2996.  
  2997.     MOV     AX, [BP].CP_SourceP ; Get Source Page #
  2998.     CMP     AX, LAST_PAGE       ; is it > Max Page #?
  2999.     JAE     @CP_Exit            ; if so, abort
  3000.  
  3001.     MOV     BX, [BP].CP_DestP   ; Get Destination Page #
  3002.     CMP     BX, LAST_PAGE       ; is it > Max Page #?
  3003.     JAE     @CP_Exit            ; if so, abort
  3004.  
  3005.     CMP     AX, BX              ; Pages #'s the same?
  3006.     JE      @CP_Exit            ; if so, abort
  3007.  
  3008.     ; Setup DS:SI and ES:DI to Video Pages
  3009.  
  3010.     SHL     BX, 1               ; Scale index to Word
  3011.     MOV     DI, PAGE_ADDR[BX]   ; Offset to Dest Page
  3012.  
  3013.     MOV     BX, AX              ; Index to Source page
  3014.     SHL     BX, 1               ; Scale index to Word
  3015.     MOV     SI, PAGE_ADDR[BX]   ; Offset to Source Page
  3016.  
  3017.     MOV     CX, PAGE_SIZE       ; Get size of Page
  3018.     MOV     AX, CURRENT_SEGMENT ; Get Video Mem Segment
  3019.     MOV     ES, AX              ; ES:DI -> Dest Page
  3020.     MOV     DS, AX              ; DS:SI -> Source Page
  3021.  
  3022.     ; Setup VGA registers for Mem to Mem copy
  3023.  
  3024.     OUT_16  GC_Index, LATCHES_ON    ; Data from Latches = on
  3025.     OUT_16  SC_Index, ALL_PLANES_ON ; Copy all Planes
  3026.  
  3027.     ; Note.. Do *NOT* use MOVSW or MOVSD - they will
  3028.     ; Screw with the latches which are 8 bits x 4
  3029.  
  3030.     REP     MOVSB               ; Copy entire Page!
  3031.  
  3032.     ; Reset VGA for normal memory access
  3033.  
  3034.     OUT_16  GC_Index, LATCHES_OFF   ; Data from Latches = off
  3035.  
  3036. @CP_Exit:
  3037.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  3038.     RET     4                   ; Exit and Clean up Stack
  3039.  
  3040. COPY_PAGE   ENDP
  3041.  
  3042.  
  3043. ;==========================================================================
  3044. ;COPY_BITMAP (SourcePage%, X1%, Y1%, X2%, Y2%, DestPage%, DestX1%, DestY1%)
  3045. ;==========================================================================
  3046. ;
  3047. ; Copies a Bitmap Image from one Display Page to Another
  3048. ; This Routine is Limited to copying Images with the same
  3049. ; Plane Alignment.  To Work: (X1 MOD 4) must = (DestX1 MOD 4)
  3050. ; Copying an Image to the Same Page is supported, but results
  3051. ; may be defined when the when the rectangular areas
  3052. ; (X1, Y1) - (X2, Y2) and (DestX1, DestY1) -
  3053. ; (DestX1+(X2-X1), DestY1+(Y2-Y1)) overlap...
  3054. ; No Paramter checking to done to insure that
  3055. ; X2 >= X1 and Y2 >= Y1.  Be Careful...
  3056. ;
  3057. ; ENTRY: SourcePage = Display Page # with Source Image
  3058. ;        X1         = Upper Left Xpos of Source Image
  3059. ;        Y1         = Upper Left Ypos of Source Image
  3060. ;        X2         = Lower Right Xpos of Source Image
  3061. ;        Y2         = Lower Right Ypos of Source Image
  3062. ;        DestPage   = Display Page # to copy Image to
  3063. ;        DestX1     = Xpos to Copy UL Corner of Image to
  3064. ;        DestY1     = Ypos to Copy UL Corner of Image to
  3065. ;
  3066. ; EXIT:  AX = Success Flag:   0 = Failure / -1= Success
  3067. ;
  3068.  
  3069. CB_STACK    STRUC
  3070.     CB_Height   DW  ?   ; Height of Image in Lines
  3071.     CB_Width    DW  ?   ; Width of Image in "bands"
  3072.                 DW  ?x4 ; DI, SI, DS, BP
  3073.                 DD  ?   ; Caller
  3074.     CB_DestY1   DW  ?   ; Destination Ypos
  3075.     CB_DestX1   DW  ?   ; Destination Xpos
  3076.     CB_DestP    DW  ?   ; Page to Copy Bitmap To
  3077.     CB_Y2       DW  ?   ; LR Ypos of Image
  3078.     CB_X2       DW  ?   ; LR Xpos of Image
  3079.     CB_Y1       DW  ?   ; UL Ypos of Image
  3080.     CB_X1       DW  ?   ; UL Xpos of Image
  3081.     CB_SourceP  DW  ?   ; Page containing Source Bitmap
  3082. CB_STACK    ENDS
  3083.  
  3084.         PUBLIC    COPY_BITMAP
  3085.  
  3086. COPY_BITMAP PROC    FAR
  3087.  
  3088.     PUSHx   BP, DS, SI, DI      ; Preserve Important Registers
  3089.     SUB     SP, 4               ; Allocate WorkSpace on Stack
  3090.     MOV     BP, SP              ; Set up Stack Frame
  3091.  
  3092.     ; Prep Registers (and keep jumps short!)
  3093.  
  3094.     MOV     ES, CURRENT_SEGMENT ; ES -> VGA Ram
  3095.     CLD                         ; Block Xfer Forwards
  3096.  
  3097.     ; Make sure Parameters are valid
  3098.  
  3099.     MOV     BX, [BP].CB_SourceP ; Get Source Page #
  3100.     CMP     BX, LAST_PAGE       ; is it > Max Page #?
  3101.     JAE     @CB_Abort           ; if so, abort
  3102.  
  3103.     MOV     CX, [BP].CB_DestP   ; Get Destination Page #
  3104.     CMP     CX, LAST_PAGE       ; is it > Max Page #?
  3105.     JAE     @CB_Abort           ; if so, abort
  3106.  
  3107.     MOV     AX, [BP].CB_X1      ; Get Source X1
  3108.     XOR     AX, [BP].CB_DestX1  ; Compare Bits 0-1
  3109.     AND     AX, PLANE_BITS      ; Check Plane Bits
  3110.     JNZ     @CB_Abort           ; They should cancel out
  3111.  
  3112.     ; Setup for Copy processing
  3113.  
  3114.     OUT_8   SC_INDEX, MAP_MASK      ; Set up for Plane Select
  3115.     OUT_16  GC_Index, LATCHES_ON    ; Data from Latches = on
  3116.  
  3117.     ; Compute Info About Images, Setup ES:SI & ES:DI
  3118.  
  3119.     MOV     AX, [BP].CB_Y2      ; Height of Bitmap in lines
  3120.     SUB     AX, [BP].CB_Y1      ; is Y2 - Y1 + 1
  3121.     INC     AX                  ; (add 1 since were not 0 based)
  3122.     MOV     [BP].CB_Height, AX  ; Save on Stack for later use
  3123.  
  3124.     MOV     AX, [BP].CB_X2      ; Get # of "Bands" of 4 Pixels
  3125.     MOV     DX, [BP].CB_X1      ; the Bitmap Occupies as X2-X1
  3126.     SHR     AX, 2               ; Get X2 Band (X2 / 4)
  3127.     SHR     DX, 2               ; Get X1 Band (X1 / 4)
  3128.     SUB     AX, DX              ; AX = # of Bands - 1
  3129.     INC     AX                  ; AX = # of Bands
  3130.     MOV     [BP].CB_Width, AX   ; Save on Stack for later use
  3131.  
  3132.     SHL     BX, 1               ; Scale Source Page to Word
  3133.     MOV     SI, PAGE_ADDR[BX]   ; SI = Offset of Source Page
  3134.     MOV     AX, [BP].CB_Y1      ; Get Source Y1 Line
  3135.     MUL     SCREEN_WIDTH        ; AX = Offset to Line Y1
  3136.     ADD     SI, AX              ; SI = Offset to Line Y1
  3137.     MOV     AX, [BP].CB_X1      ; Get Source X1
  3138.     SHR     AX, 2               ; X1 / 4 = Byte offset
  3139.     ADD     SI, AX              ; SI = Byte Offset to (X1,Y1)
  3140.  
  3141.     MOV     BX, CX              ; Dest Page Index to BX
  3142.     SHL     BX, 1               ; Scale Source Page to Word
  3143.     MOV     DI, PAGE_ADDR[BX]   ; DI = Offset of Dest Page
  3144.     MOV     AX, [BP].CB_DestY1  ; Get Dest Y1 Line
  3145.     MUL     SCREEN_WIDTH        ; AX = Offset to Line Y1
  3146.     ADD     DI, AX              ; DI = Offset to Line Y1
  3147.     MOV     AX, [BP].CB_DestX1  ; Get Dest X1
  3148.     SHR     AX, 2               ; X1 / 4 = Byte offset
  3149.     ADD     DI, AX              ; DI = Byte Offset to (D-X1,D-Y1)
  3150.  
  3151.     MOV     CX, [BP].CB_Width   ; CX = Width of Image (Bands)
  3152.     DEC     CX                  ; CX = 1?
  3153.     JE      @CB_Only_One_Band   ; 0 Means Image Width of 1 Band
  3154.  
  3155.     MOV     BX, [BP].CB_X1      ; Get Source X1
  3156.     AND     BX, PLANE_BITS      ; Aligned? (bits 0-1 = 00?)
  3157.     JZ      @CB_Check_Right     ; if so, check right alignment
  3158.     JNZ     @CB_Left_Band       ; not aligned? well..
  3159.  
  3160. @CB_Abort:
  3161.     CLR     AX                  ; Return False (Failure)
  3162.     JMP     @CB_Exit            ; and Finish Up
  3163.  
  3164.     ; Copy when Left & Right Clip Masks overlap...
  3165.  
  3166. @CB_Only_One_Band:
  3167.     MOV     BX, [BP].CB_X1          ; Get Left Clip Mask
  3168.     AND     BX, PLANE_BITS          ; Mask out Row #
  3169.     MOV     AL, Left_Clip_Mask[BX]  ; Get Left Edge Mask
  3170.     MOV     BX, [BP].CB_X2          ; Get Right Clip Mask
  3171.     AND     BX, PLANE_BITS          ; Mask out Row #
  3172.     AND     AL, Right_Clip_Mask[BX] ; Get Right Edge Mask byte
  3173.  
  3174.     OUT_8   SC_Data, AL         ; Clip For Left & Right Masks
  3175.  
  3176.     MOV     CX, [BP].CB_Height  ; CX = # of Lines to Copy
  3177.     MOV     DX, SCREEN_WIDTH    ; DX = Width of Screen
  3178.     CLR     BX                  ; BX = Offset into Image
  3179.  
  3180. @CB_One_Loop:
  3181.     MOV     AL, ES:[SI+BX]      ; Load Latches
  3182.     MOV     ES:[DI+BX], AL      ; Unload Latches
  3183.     ADD     BX, DX              ; Advance Offset to Next Line
  3184.     LOOPjz  CX, @CB_One_Done    ; Exit Loop if Finished
  3185.  
  3186.     MOV     AL, ES:[SI+BX]      ; Load Latches
  3187.     MOV     ES:[DI+BX], AL      ; Unload Latches
  3188.     ADD     BX, DX              ; Advance Offset to Next Line
  3189.     LOOPx   CX, @CB_One_Loop    ; Loop until Finished
  3190.  
  3191. @CB_One_Done:
  3192.     JMP     @CB_Finish          ; Outa Here!
  3193.  
  3194.     ; Copy Left Edge of Bitmap
  3195.  
  3196. @CB_Left_Band:
  3197.  
  3198.     OUT_8   SC_Data, Left_Clip_Mask[BX] ; Set Left Edge Plane Mask
  3199.  
  3200.     MOV     CX, [BP].CB_Height  ; CX = # of Lines to Copy
  3201.     MOV     DX, SCREEN_WIDTH    ; DX = Width of Screen
  3202.     CLR     BX                  ; BX = Offset into Image
  3203.  
  3204. @CB_Left_Loop:
  3205.     MOV     AL, ES:[SI+BX]      ; Load Latches
  3206.     MOV     ES:[DI+BX], AL      ; Unload Latches
  3207.     ADD     BX, DX              ; Advance Offset to Next Line
  3208.     LOOPjz  CX, @CB_Left_Done   ; Exit Loop if Finished
  3209.  
  3210.     MOV     AL, ES:[SI+BX]      ; Load Latches
  3211.     MOV     ES:[DI+BX], AL      ; Unload Latches
  3212.     ADD     BX, DX              ; Advance Offset to Next Line
  3213.     LOOPx   CX, @CB_Left_Loop   ; Loop until Finished
  3214.  
  3215. @CB_Left_Done:
  3216.     INC     DI                  ; Move Dest Over 1 band
  3217.     INC     SI                  ; Move Source Over 1 band
  3218.     DEC     [BP].CB_Width       ; Band Width--
  3219.  
  3220.     ; Determine if Right Edge of Bitmap needs special copy
  3221.  
  3222. @CB_Check_Right:
  3223.     MOV     BX, [BP].CB_X2      ; Get Source X2
  3224.     AND     BX, PLANE_BITS      ; Aligned? (bits 0-1 = 11?)
  3225.     CMP     BL, 03h             ; Plane = 3?
  3226.     JE      @CB_Copy_Middle     ; Copy the Middle then!
  3227.  
  3228.     ; Copy Right Edge of Bitmap
  3229.  
  3230. @CB_Right_Band:
  3231.  
  3232.     OUT_8   SC_Data, Right_Clip_Mask[BX]    ; Set Right Edge Plane Mask
  3233.  
  3234.     DEC     [BP].CB_Width       ; Band Width--
  3235.     MOV     CX, [BP].CB_Height  ; CX = # of Lines to Copy
  3236.     MOV     DX, SCREEN_WIDTH    ; DX = Width of Screen
  3237.     MOV     BX, [BP].CB_Width   ; BX = Offset to Right Edge
  3238.  
  3239. @CB_Right_Loop:
  3240.     MOV     AL, ES:[SI+BX]      ; Load Latches
  3241.     MOV     ES:[DI+BX], AL      ; Unload Latches
  3242.     ADD     BX, DX              ; Advance Offset to Next Line
  3243.     LOOPjz  CX, @CB_Right_Done  ; Exit Loop if Finished
  3244.  
  3245.     MOV     AL, ES:[SI+BX]      ; Load Latches
  3246.     MOV     ES:[DI+BX], AL      ; Unload Latches
  3247.     ADD     BX, DX              ; Advance Offset to Next Line
  3248.     LOOPx   CX, @CB_Right_Loop  ; Loop until Finished
  3249.  
  3250. @CB_Right_Done:
  3251.  
  3252.     ; Copy the Main Block of the Bitmap
  3253.  
  3254. @CB_Copy_Middle:
  3255.  
  3256.     MOV     CX, [BP].CB_Width   ; Get Width Remaining
  3257.     JCXZ    @CB_Finish          ; Exit if Done
  3258.  
  3259.     OUT_8   SC_Data, ALL_PLANES ; Copy all Planes
  3260.  
  3261.     MOV     DX, SCREEN_WIDTH    ; Get Width of Screen minus
  3262.     SUB     DX, CX              ; Image width (for Adjustment)
  3263.     MOV     AX, [BP].CB_Height  ; AX = # of Lines to Copy
  3264.     MOV     BX, CX              ; BX = Quick REP reload count
  3265.     MOV     CX, ES              ; Move VGA Segment
  3266.     MOV     DS, CX              ; Into DS
  3267.  
  3268.     ; Actual Copy Loop.  REP MOVSB does the work
  3269.  
  3270. @CB_Middle_Copy:
  3271.     MOV     CX, BX              ; Recharge Rep Count
  3272.     REP     MOVSB               ; Move Bands
  3273.     LOOPjz  AX, @CB_Finish      ; Exit Loop if Finished
  3274.  
  3275.     ADD     SI, DX              ; Adjust DS:SI to Next Line
  3276.     ADD     DI, DX              ; Adjust ES:DI to Next Line
  3277.  
  3278.     MOV     CX, BX              ; Recharge Rep Count
  3279.     REP     MOVSB               ; Move Bands
  3280.  
  3281.     ADD     SI, DX              ; Adjust DS:SI to Next Line
  3282.     ADD     DI, DX              ; Adjust ES:DI to Next Line
  3283.     LOOPx   AX, @CB_Middle_Copy ; Copy Lines until Done
  3284.  
  3285. @CB_Finish:
  3286.     OUT_16  GC_Index, LATCHES_OFF   ; Data from Latches = on
  3287.  
  3288. @CB_Exit:
  3289.     ADD     SP, 04              ; Deallocate stack workspace
  3290.     POPx    DI, SI, DS, BP      ; Restore Saved Registers
  3291.     RET     16                  ; Exit and Clean up Stack
  3292.  
  3293. COPY_BITMAP ENDP
  3294.  
  3295.     END                         ; End of Code Segment
  3296.